I am trying to write a function that converts a non-negative integer to its English word representation. For example:
convertNumToEnglishLetters(123) // "One Hundred Twenty Three"
convertNumToEnglishLetters(999) // "Nine Hundred Nighty Nine"
convertNumToEnglishLetters(3333) // 'Three Thounsand Three Hundred Thirty Three'
convertNumToEnglishLetters(12312) // Twelve Thousand Three Hundred Twelve
Here is my attempt:
const ENGLISH_LETTER_BY_NUM = {
'1' : 'One',
'2': 'Two',
'3': 'Three',
//... omit some numbers for brevity
'10': 'Ten',
'11': 'Evelen',
'13': 'ThirdTeen',
'20': 'Twenty',
'30': 'Thirty',
//... omit some numbers for brevity
'00': 'Hundred',
'000': 'Thounsand'
}
function convertNumToEnglishLetters(num) {
const numStr = num.toString()
const englishLettersArr = []
for(let i = numStr.length -1; i >= 0; i--) {
let numChar = numStr[i]
let englishLetter = ''
if(i === numStr.length - 1) {
if(numChar === '0') continue
englishLetter = ENGLISH_LETTER_BY_NUM[numChar]
} else if ( i === numStr.length - 2) {
if(numChar === '0') continue
if(numChar === '1') {
numChar += numStr[i+1]
englishLettersArr.pop()
} else {
numChar += '0'
}
englishLetter = ENGLISH_LETTER_BY_NUM[numChar]
} else if( i === numStr.length - 3 ) {
const HUNREND = ENGLISH_LETTER_BY_NUM['00']
// numChar += '00'
englishLetter = ENGLISH_LETTER_BY_NUM[numChar] + ' ' + HUNREND
} else if( i === numStr.length - 4) {
const THOUNSAND = ENGLISH_LETTER_BY_NUM['000']
englishLetter = ENGLISH_LETTER_BY_NUM[numChar] + ' ' + THOUNSAND
} else {
const rest = numStr.slice(0, numStr.length - 3)
englishLetter += convertNumToEnglishLetters(Number(rest))
}
englishLettersArr.push(englishLetter)
}
return englishLettersArr.reverse().join(' ')
}
I built a map ENGLISH_LETTER_BY_NUM
with a minimal set of hard coded English letters. My solution works for numbers that are under 10000. But once the number goes past that, my solution wouldn't work anymore. I guess I need to recursively call the function to calculate part of the number when the length of the number is bigger than 4. But right now the solution is not working properly.