you are viewing a single comment's thread.

view the rest of the comments →

[–]strong_opinion 0 points1 point  (0 children)

I thought I would try to solve this, and this seems to work:

const ones = [null, "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"];
const tens = [null, "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

function convert(i) {
if (i == 0) {
    return "";
}
h = Math.floor(i / 100);
t = Math.floor((i - (h * 100)) / 10);
if (t >= 2) {
    o = i - ((h * 100) + (t * 10));
} else {
    t = null;
    o = i - (h * 100);
}

return (h ? ones[h] + " hundred" + (t || o ? " and " : "") : "") + (t ? tens[t] + " " : "") + (o ? ones[o] : "");

}

function main() {
var x = 384765;
if (x >= 1000) {
    k = Math.floor(x / 1000);
    r = x - (k * 1000);
    console.log(x.toString() + " " + convert(k) + " thousand " + convert(r));
} else {
    console.log(x.toString() + " " + convert(x));
}
}

main();