Created
April 7, 2016 21:59
-
-
Save icodejs/50ee9cfdaef82433180bb8ff5266230a to your computer and use it in GitHub Desktop.
Write out numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a function that transforms any positive number to a string representing the number in words. The function should work for all numbers between 0 and 999999. | |
For example, | |
number2words(0) should return "zero" | |
number2words(1) should return "one" | |
number2words(9) should return "nine" | |
number2words(10) should return "ten" | |
number2words(17) should return "seventeen" | |
number2words(20) should return "twenty" | |
number2words(21) should return "twenty-one" | |
number2words(45) should return "forty-five" | |
number2words(80) should return "eighty" | |
number2words(99) should return "ninety-nine" | |
number2words(100) should return "one hundred" | |
number2words(301) should return "three hundred one" | |
number2words(799) should return "seven hundred ninety-nine" | |
number2words(800) should return "eight hundred" | |
number2words(950) should return "nine hundred fifty" | |
number2words(1000) should return "one thousand" | |
number2words(1002) should return "one thousand two" | |
number2words(3051) should return "three thousand fifty-one" | |
number2words(7200) should return "seven thousand two hundred" | |
number2words(7219) should return "seven thousand two hundred nineteen" | |
number2words(8330) should return "eight thousand three hundred thirty" | |
number2words(99999) should return "ninety-nine thousand nine hundred ninety-nine" | |
number2words(888888) should return "eight hundred eighty-eight thousand eight hundred eighty-eight" | |
*/ | |
function number2words(n){ | |
const baseNumbers = { | |
0: 'zero', | |
1: 'one', | |
2: 'two', | |
3: 'three', | |
4: 'four', | |
5: 'five', | |
6: 'six', | |
7: 'seven', | |
8: 'eight', | |
9: 'nine', | |
10: 'ten', | |
11: 'eleven', | |
12: 'twelve', | |
13: 'thirteen', | |
14: 'fourteen', | |
15: 'fifteen', | |
16: 'sixteen', | |
17: 'seventeen', | |
18: 'eighteen', | |
19: 'nineteen', | |
20: 'twenty', | |
30: 'thirty', | |
40: 'forty', | |
50: 'fifty', | |
60: 'sixty', | |
70: 'seventy', | |
80: 'eighty', | |
90: 'ninety', | |
100: 'hundred', | |
1000: 'thousand' | |
}; | |
const first = (n, i) => String.prototype.substring.call(n, 0, i); | |
const rest = (n, i) => String.prototype.substring.call(n, i); | |
const calculate = (n, acceptZero = true) => { | |
console.log('calculate', n); | |
if (n === 0 && !acceptZero) return ''; | |
if (n < 100 && baseNumbers[n]) return baseNumbers[n]; | |
switch (String(n).length) { | |
case 2: return tens(n); | |
case 3: return aboveOneHundred(n, 1, 100); | |
case 4: return aboveOneHundred(n, 1, 1000); | |
case 5: return aboveOneHundred(n, 2, 1000); | |
case 6: return aboveOneHundred(n, 3, 1000); | |
} | |
} | |
const tens = (n) => { | |
const v = parseInt(n, 10); | |
return [ | |
calculate(first(v, 1)*10), | |
calculate(rest(v, 1)) | |
].join('-'); | |
} | |
const aboveOneHundred = (n, i, baseNumber) => { | |
const v = parseInt(n, 10); | |
const tail = parseInt(rest(v, i), 10); | |
return [ | |
calculate(first(v, i)), | |
baseNumbers[baseNumber], | |
calculate(tail, false) | |
].join(' ').trim();; | |
} | |
return calculate(n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment