Skip to content

Instantly share code, notes, and snippets.

@e-mihaylin
Created June 19, 2018 12:13
Show Gist options
  • Save e-mihaylin/bf9a84960c05c07fed9d44fdf659790e to your computer and use it in GitHub Desktop.
Save e-mihaylin/bf9a84960c05c07fed9d44fdf659790e to your computer and use it in GitHub Desktop.
const parseInt = string => {
var n = 0,
g = 0;
function text2num(s) {
const a = s.toString().split(/[\s-]+/);
a.forEach(feach);
return n + g;
}
const feach = w => {
let x = numbers[w];
if (x) g = g + x;
else if (w == 'hundred') g = g * 100;
else {
x = magnitude[w];
if (x) {
n = n + g * x;
g = 0;
}
}
}
return text2num(string);
}
const numbers = {
'zero': 0,
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
'eleven': 11,
'twelve': 12,
'thirteen': 13,
'fourteen': 14,
'fifteen': 15,
'sixteen': 16,
'seventeen': 17,
'eighteen': 18,
'nineteen': 19,
'twenty': 20,
'thirty': 30,
'forty': 40,
'fifty': 50,
'sixty': 60,
'seventy': 70,
'eighty': 80,
'ninety': 90
};
const magnitude = {
'thousand': 1000,
'million': 1000000,
'billion': 1000000000,
'trillion': 1000000000000,
'quadrillion': 1000000000000000,
'quintillion': 1000000000000000000,
'sextillion': 1000000000000000000000,
'septillion': 1000000000000000000000000,
'octillion': 1000000000000000000000000000,
'nonillion': 1000000000000000000000000000000,
'decillion': 1000000000000000000000000000000000
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment