Num to words

by esantiagovieira

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<body>
  <div class="container">
    <input type="text" class="form-control" id="number" onkeyup="update();" /*this code prevent non numeric letters*/ onkeydown="return (event.ctrlKey || event.altKey 
                    || (47<event.keyCode && event.keyCode<58 && event.shiftKey==false) 
                    || (95<event.keyCode && event.keyCode<106)
                    || (event.keyCode==8) || (event.keyCode==9) 
                    || (event.keyCode>34 && event.keyCode<40) 
                    || (event.keyCode==46) )" />
    <br/>
    <div id="container">Here The Numbers Printed</div>
  </div>
</body>

CSS

.container {
  margin-top: 8px;
}
/*  
    RUN WITH THIS
    43454315 
*/

JavaScript

function update() {
  var bigNumArry = new Array('', ' Thousand', ' Million', ' Billion', ' Trillion', ' Quadrillion', ' Quintillion');

  var output = '';
  var numString = document.getElementById('number').value;
  var finlOutPut = new Array();

  if (numString == '0') {
    document.getElementById('container').innerHTML = 'Zero';
    return;
  }

  if (numString == 0) {
    document.getElementById('container').innerHTML = 'Please enter only numbers';
    return;
  }

  var i = numString.length;
  i = i - 1;

  //cut the number to grups of three digits and add them to the Arry
  while (numString.length > 3) {
    var triDig = new Array(3);
    triDig[2] = numString.charAt(numString.length - 1);
    triDig[1] = numString.charAt(numString.length - 2);
    triDig[0] = numString.charAt(numString.length - 3);

    var varToAdd = triDig[0] + triDig[1] + triDig[2];
    finlOutPut.push(varToAdd);
    i--;
    numString = numString.substring(0, numString.length - 3);
  }
  finlOutPut.push(numString);
  finlOutPut.reverse();

  //conver each grup of three digits to english word
  //if all digits are zero the triConvert
  //function return the string "dontAddBigSufix"
  for (j = 0; j < finlOutPut.length; j++) {
    finlOutPut[j] = triConvert(parseInt(finlOutPut[j], 10));
  }

  var bigScalCntr = 0; //this int mark the million billion trillion... Arry

  for (b = finlOutPut.length - 1; b >= 0; b--) {
    if (finlOutPut[b] != "dontAddBigSufix") {
      finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] +'';
      bigScalCntr++;
    } else {
      //replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
      finlOutPut[b] = ' ';
      bigScalCntr++; //advance the counter  
    }
  }

  //convert The output Arry to , more printable string 
  for (n = 0; n < finlOutPut.length; n++) {
    output += finlOutPut[n];
  }

  document.getElementById('container').innerHTML = output; //print the output
}

//simple function to convert from numbers to words from 1...