This parses two digits and returns the rest of the string.
Input:
"20asg"
Output:
{ number: 20, left: "asg" }
Input:
"9asg"
Output:
{ number: 9, left: "asg" }
function readDigit(str) {
var digitMap = {
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9
};
var number1 = digitMap[str[0]];
var number2 = digitMap[str[1]];
if (number2 === undefined) {
if (number1 === undefined) {
return {
left: str.slice(1)
};
}
return {
number: number1,
left: str.slice(1)
};
} else {
return {
number: (number1 * 10) + number2,
left: str.slice(2)
};
}
}