function NumToWordsInt(NumIn) {
//-------------------------------------------------------
//Convert Integer Number to English Words
//Using a Single Loop String Triplets (SLST) Methods
//Mohsen Alyafei 10 July 2019
//Call it for a whole number and fractional separately
//-------------------------------------------------------
if (NumIn==0) return "Zero";
var Ones = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
var Tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
var Scale = ["", "Thousand", "Million", "Billion", "Trillion", "Quadrillion", "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion", "Decillion"];
var N1, N2, Sep, L, j, i, h,Trplt,tns="", NumAll = "";
NumIn += ""; //NumIn=NumIn.toString()
//----------------- code start -------------------
NumIn = "0".repeat(NumIn.length * 2 % 3) + NumIn; //Create shortest string triplets 0 padded
j = 0; //Start with the highest triplet from LH
for (i = NumIn.length / 3 - 1; i >= 0; i--) { //Loop thru number of triplets from LH most
Trplt = NumIn.substring(j, j + 3); //Get a triplet number starting from LH
if (Trplt != "000") { //Skip empty triplets
h = ""; //Init hundreds //-------inner code for 3 digit1 startstriplet
Trplt[2] != "0" ? Sep="-":Sep=" "; //Only if hyphen needed for nums 21 to 99
N1 = Number(Trplt[0]); //Get Hundreds digit
N2 = Number(Trplt.substr(1)); //Get 2 lowest digits (00 to 99)
N2 > 19 ? tns = Tens[Number(Trplt[1])] + Sep + Ones[Number(Trplt[2])]:tns = Ones[N2]
if (N1 > 0) h = Ones[N1] + " Hundred" //Add " hundred" if needed
Trplt = (h + " " + tns).trim() + " " + Scale[i]; //Create number with scale ----inner code ends
NumAll = NumAll + Trplt + " "; //join the triplets scales to previous
}
j += 3; //Go for next lower triplets (move to RH)
}
//----------------- code end ---------------------
return NumAll.trim(); //Return trimming excess spaces
}
//
//
//================= for testing ================
document.getElementById('number').onkeyup = function () {
document.getElementById('words').innerHTML = NumToWordsInt(document.getElementById('number').value);
};
<span id="words"></span>
<input id="number" type="text" />