Code:
function getDigits(n){
var result = "enter a number"
var characters = n + "" // converts n to a string
switch (characters.length){
case 1:
switch(n){
case 1: result = 'one';break;
case 2: result = 'two';break;
case 3: result = 'three';break;
case 4: result = 'four';break;
case 5: result = 'five';break;
case 6: result = 'six';break;
case 7: result = 'seven';break;
case 8: result = 'eight';break;
case 9: result = 'nine';break;
default: result = '';break;
}break;
case 2:
switch (Math.floor(n / 10))
{
case 1:
result = "ten" + " " + getDigits(n % 10);
break;
case 2:
result = "twenty" + " " + getDigits(n % 10); break;
case 3:
result = "thirty" + " " + getDigits(n % 10); break;
case 4:
result = "forty" + " " + getDigits(n % 10); break;
case 5:
result = "fifty" + " " + getDigits(n % 10); break;
case 6:
result = "Sixty" + " " + getDigits(n % 10); break;
case 7:
result = "seventy" + " " + getDigits(n % 10); break;
case 8:
result = "eighty" + " " + getDigits(n % 10); break;
case 9:
result = "ninety" + " " + getDigits(n % 10); break;
default:
result = ""; break;
}
break;
case 3:
switch (Math.floor(n / 100))
{
case 1:
result = "one hundred" + " " + getDigits(n % 100);
break;
case 2:
result = "two hundred" + " " + getDigits(n % 100); break;
case 3:
result = "three hundred" + " " + getDigits(n % 100); break;
case 4:
result = "four hundred" + " " + getDigits(n % 100); break;
case 5:
result = "five hundred" + " " + getDigits(n % 100); break;
case 6:
result = "six hundred" + " " + getDigits(n % 100); break;
case 7:
result = "seven hundred" + " " + getDigits(n % 100); break;
case 8:
result = "eight hundred" + " " + getDigits(n % 100); break;
case 9:
result = "nine hundred" + " " + getDigits(n % 100); break;
default:
result = ""; break;
}
break;
case 4:
switch (Math.floor(n / 1000))
{
case 1:
result = "one Thousand" + " " + getDigits(n % 1000);
break;
case 2:
result = "two Thousand" + " " + getDigits(n % 1000); break;
case 3:
result = "three Thousand" + " " + getDigits(n % 1000); break;
case 4:
result = "four Thousand" + " " + getDigits(n % 1000); break;
case 5:
result = "five Thousand" + " " + getDigits(n % 1000); break;
case 6:
result = "six Thousand" + " " + getDigits(n % 1000); break;
case 7:
result = "seven Thousand" + " " + getDigits(n % 1000); break;
case 8:
result = "eight Thousand" + " " + getDigits(n % 1000); break;
case 9:
result = "nine Thousand" + " " + getDigits(n % 1000); break;
default:
result = ""; break;
}
break;
case 5:
var temp1 = getDigits(Math.floor(n/1000)) + " thousand"; temp1 = temp1 + " " + getDigits(n% 1000);
result = temp1;
break;
case 6:
var temp1 = getDigits(Math.floor(n/1000))+ " thousand";
temp1 = temp1 + " " + getDigits(n% 1000);
result = temp1;
break;
case 7:
switch (Math.floor(n / 1000000))
{
case 1:
result = "one million" + " " + getDigits(n % 1000000);
break;
case 2:
result = "two million" + " " + getDigits(n % 1000000); break;
case 3:
result = "three million" + " " + getDigits(n % 1000000); break;
case 4:
result = "four million" + " " + getDigits(n % 1000000); break;
case 5:
result = "five million" + " " + getDigits(n % 1000000); break;
case 6:
result = "six million" + " " + getDigits(n % 1000000); break;
case 7:
result = "seven million" + " " + getDigits(n % 1000000); break;
case 8:
result = "eight million" + " " + getDigits(n % 1000000); break;
case 9:
result = "nine million" + " " + getDigits(n % 1000000); break;
default:
result = ""; break;
}
break;
case 8:
var temp1 = getDigits(Math.floor(n/1000000))+ " million";
temp1 = temp1 + " " + getDigits(n% 1000000);
result = temp1;
break;
case 9:
var temp1 = getDigits(Math.floor(n/1000000))+ " million";
temp1 = temp1 + " " + getDigits(n% 1000000);
result = temp1;
break;
case 10:
switch (Math.floor(n / 1000000000))
{
case 1:
result = "one Billion" + " " + getDigits(n % 1000000000);
break;
case 2:
result = "two Billion" + " " + getDigits(n % 1000000000); break;
case 3:
result = "three Billion" + " " + getDigits(n % 1000000000); break;
case 4:
result = "four Billion" + " " + getDigits(n % 1000000000); break;
case 5:
result = "five Billion" + " " + getDigits(n % 1000000000); break;
case 6:
result = "six Billion" + " " + getDigits(n % 1000000000); break;
case 7:
result = "seven Billion" + " " + getDigits(n % 1000000000); break;
case 8:
result = "eight Billion" + " " + getDigits(n % 1000000000); break;
case 9:
result = "nine Billion" + " " + getDigits(n % 1000000000); break;
default:
result = ""; break;
}
break;
case 11:case 12 :
var temp1 = getDigits(Math.floor(n/1000000000))+ " Billion";
temp1 = temp1 + " " + getDigits(n% 1000000000);
result = temp1;
break;
case 13:
switch (Math.floor(n / 1000000000000))
{
case 1:
result = "one Trillion" + " " + getDigits(n % 1000000000000);
break;
case 2:
result = "two Trillion" + " " + getDigits(n % 1000000000000); break;
case 3:
result = "three Trillion" + " " + getDigits(n % 1000000000000); break;
case 4:
result = "four Trillion" + " " + getDigits(n % 1000000000000); break;
case 5:
result = "five Trillion" + " " + getDigits(n % 1000000000000); break;
case 6:
result = "six Trillion" + " " + getDigits(n % 1000000000000); break;
case 7:
result = "seven Trillion" + " " + getDigits(n % 1000000000000); break;
case 8:
result = "eight Trillion" + " " + getDigits(n % 1000000000000); break;
case 9:
result = "nine Trillion" + " " + getDigits(n % 1000000000000); break;
default:
result = ""; break;
}
break;
case 14:case 15 :
var temp1 = getDigits(Math.floor(n/1000000000000))+ " Trillion";
temp1 = temp1 + " " + getDigits(n% 1000000000000);
result = temp1;
break;
case 16:
switch (Math.floor(n / 1000000000000000))
{
case 1:
result = "one Quadrillion" + " " + getDigits(n % 1000000000000000);
break;
case 2:
result = "two Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 3:
result = "three Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 4:
result = "four Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 5:
result = "five Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 6:
result = "six Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 7:
result = "seven Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 8:
result = "eight Quadrillion" + " " + getDigits(n % 1000000000000000); break;
case 9:
result = "nine Quadrillion" + " " + getDigits(n % 1000000000000000); break;
default:
result = ""; break;
}
break;
case 17:case 18 :
var temp1 = getDigits(Math.floor(n/1000000000000000))+ " Quadrillion";
temp1 = temp1 + " " + getDigits(n% 1000000000000000);
result = temp1;
break;
case 19:
switch (Math.floor(n / 1000000000000000000))
{
case 1:
result = "one Quintillion" + " " + getDigits(n % 1000000000000000000);
break;
case 2:
result = "two Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 3:
result = "three Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 4:
result = "four Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 5:
result = "five Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 6:
result = "six Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 7:
result = "seven Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 8:
result = "eight Quintillion" + " " + getDigits(n % 1000000000000000000); break;
case 9:
result = "nine Quintillion" + " " + getDigits(n % 1000000000000000000); break;
default:
result = ""; break;
}
break;
case 20:case 21 :
var temp1 = getDigits(Math.floor(n/1000000000000000000))+ " Quintillion";
temp1 = temp1 + " " + getDigits(n% 1000000000000000000);
result = temp1;
break;
}
return result
}
console.info(getDigits(666666666666666666666))
function repairText(n){
var res = n;
res = res.replace("ten one", "eleven");
res = res.replace("ten two", "twelve");
res = res.replace("ten one", "thirteen");
res = res.replace("ten one", "fourteen");
res = res.replace("ten one", "fifteen");
res = res.replace("ten one", "sixteen");
res = res.replace("ten one", "seventeen");
res = res.replace("ten one", "eighteen");
res = res.replace("ten one", "nineteen");
return res;
}
function getDigit(n){
return repairText(getDigits(n))}
console.info(getDigit(12))