Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem is already solved but I am posting another way of doing it just for reference.</p> <p>The code was written to be tested on node.js, but the functions should work fine when called within the browser. Also, this only handles the range [0,1000000], but can be easily adapted for bigger ranges.</p> <pre><code>#! /usr/bin/env node var sys=require('sys'); // actual conversion code starts here var ones=['','one','two','three','four','five','six','seven','eight','nine']; var tens=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']; var teens=['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']; function convert_millions(num){ if (num&gt;=1000000){ return convert_millions(Math.floor(num/1000000))+" million "+convert_thousands(num%1000000); } else { return convert_thousands(num); } } function convert_thousands(num){ if (num&gt;=1000){ return convert_hundreds(Math.floor(num/1000))+" thousand "+convert_hundreds(num%1000); } else{ return convert_hundreds(num); } } function convert_hundreds(num){ if (num&gt;99){ return ones[Math.floor(num/100)]+" hundred "+convert_tens(num%100); } else{ return convert_tens(num); } } function convert_tens(num){ if (num&lt;10) return ones[num]; else if (num&gt;=10 &amp;&amp; num&lt;20) return teens[num-10]; else{ return tens[Math.floor(num/10)]+" "+ones[num%10]; } } function convert(num){ if (num==0) return "zero"; else return convert_millions(num); } //end of conversion code //testing code begins here function main(){ var cases=[0,1,2,7,10,11,12,13,15,19,20,21,25,29,30,35,50,55,69,70,99,100,101,119,510,900,1000,5001,5019,5555,10000,11000,100000,199001,1000000,1111111,190000009]; for (var i=0;i&lt;cases.length;i++ ){ sys.puts(cases[i]+": "+convert(cases[i])); } } main(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload