JavaScript
const letterifyNumber = number => { const singulars = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; const decens = ["", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; const teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "egighteen", "nineteen"]; const units = ["", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sixtillion", "septillion"]; const blocks = number.toString().split('').reverse().join('').match(/.{1,3}/g); let numberString = ""; for (let x = 0; x < blocks.length; x++) { let sectionString = ""; if (parseInt(blocks[x]) !== 0) { if (blocks[x].length > 2 && blocks[x][2] !== "0") { sectionString += `${singulars[blocks[x][2]]} hundred ` } if (blocks[x].length > 1) { if (blocks[x][1] === "1") { sectionString += `${teens[blocks[x][0]]} ` } else { sectionString += `${decens[blocks[x][1]]} `; } } if (blocks[x][1] !== "1") { sectionString += `${singulars[blocks[x][0]]} ` } sectionString += `${units[x]} ` numberString = sectionString + numberString; } } return numberString; }
Live demo on CodePen.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript
Live demo on CodePen.