-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path273.integer-to-english-words.js
91 lines (87 loc) · 1.71 KB
/
273.integer-to-english-words.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const BASIC = [
"Zero",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eighteen",
"Nineteen"
];
const TWENTIES = [
"",
"",
"Twenty",
"Thirty",
"Forty",
"Fifty",
"Sixty",
"Seventy",
"Eighty",
"Ninety"
];
const HUNDREDS = ["Hundred"];
const OVER_THOUSANDS = ["", "Thousand", "Million", "Billion", "Trillion"];
// num MUST BE less than 1000 , 0-999,e.g 996
var threePack = function(num) {
console.log("three pack", num);
let res = "";
//res Nine Hundread ,num before 996, after 96
if (num >= 100) {
res += BASIC[Math.floor(num / 100)] + " ";
res += HUNDREDS[0] + " ";
num %= 100;
}
//res 9,num before 996, after 96, > 20,30,40,50,60,70,80,90 mapping
if (num >= 20) {
res += TWENTIES[Math.floor(num / 10)] + " ";
num %= 10;
}
//show value when below 19
if (num > 0) {
res += BASIC[num] + " ";
}
return res;
};
/**
* @param {number} num
* @return {string}
*/
var numberNeedConversion = function(num, divider) {
if (num < divider) {
return true;
} else {
return false;
}
};
//very intuitive!
var numberToWords = function(num) {
if (num === 0) {
return BASIC[0];
}
let ans = "";
for (let i = OVER_THOUSANDS.length - 1; i >= 0; i -= 1) {
//from trillion to zero conversion
const divider = Math.pow(1000, i);
console.log("divider", divider);
if (numberNeedConversion(num, divider)) {
continue;
}
ans += threePack(Math.floor(num / divider));
ans += OVER_THOUSANDS[i] === "" ? "" : OVER_THOUSANDS[i] + " ";
num %= divider;
}
return ans.substring(0, ans.length - 1);
};