If I look at your comments above, it seems that your output should be a conversion of a number to its text representation, you need to write a separate function that performs the conversion. If so, please update your question accordingly as it is a little misleading right now. I ran your code in jsfiddle and it worked fine -
const NUMBER_WORDS = {
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"
};
function numberToText(n) {
if (n < 0 || n > 999999999) {
throw new Error("Number out of range");
}
if (n in NUMBER_WORDS) {
return NUMBER_WORDS[n];
}
let text = "";
if (n >= 1000000) {
text += numberToText(Math.floor(n / 1000000)) + " Million ";
n %= 1000000;
}
if (n >= 1000) {
text += numberToText(Math.floor(n / 1000)) + " Thousand ";
n %= 1000;
}
if (n >= 100) {
text += numberToText(Math.floor(n / 100)) + " Hundred ";
n %= 100;
}
if (n > 0) {
if (text !== "") {
text += "and ";
}
if (n in NUMBER_WORDS) {
text += NUMBER_WORDS[n];
} else {
text += NUMBER_WORDS[Math.floor(n / 10) * 10] + " ";
text += NUMBER_WORDS[n % 10];
}
}
return text.trim();
}
To use this function with the input numbers in your above code, you can call numberToText() on each number and log the result to the console -
console.log(numberToText(0));
console.log(numberToText(100));
console.log(numberToText(1000));
console.log(numberToText(10000));
console.log(numberToText(100000));
console.log(numberToText(1000000));
console.log(numberToText(10000000));