-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSay.js
More file actions
27 lines (26 loc) · 919 Bytes
/
Copy pathSay.js
File metadata and controls
27 lines (26 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*http://exercism.io/exercises/javascript/say/readme
This spells out a given integer in text*/
var num=("1234567849".split("")).reverse();
if(num[0]=="0"){
console.log("zero");
}
var numChunks=[];
var wrd="";
var finalWrd="";
var numbers=[
["one","two","three","four","five","six","seven","eight","nine"],
["onety","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninty"],
["one hundred","two hundred","three hundred","four hundred","five hundred","six hundred","seven hundred","eight hundred","nine hundred"]
];
var places=["","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion"];
while(num.length){
numChunks.push(num.splice(0,3));
}
for(var chunk in numChunks){
for(var num in numChunks[chunk]){
wrd=numbers[num][numChunks[chunk][num]-1]+" "+wrd;
}
finalWrd=wrd+places[chunk]+" "+finalWrd;
wrd="";
}
console.log(finalWrd);