This repository was archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12.js
More file actions
82 lines (71 loc) · 1.26 KB
/
day12.js
File metadata and controls
82 lines (71 loc) · 1.26 KB
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
const sayNumberInEnglish = n => {
if (n.length === 0) {
return;
}
const ones = [
'',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
];
const tens = [
'',
'',
'twenty',
'thirty',
'forty',
'fifty',
'sixty',
'seventy',
'eighty',
'ninety',
];
const sep = [
'',
' thousand ',
' million ',
' billion ',
' trillion ',
' quadrillion ',
' quintillion ',
' sextillion ',
];
const arr = [];
let str = '';
let i = 0;
n = parseInt(n, 10);
while (n) {
arr.push(n % 1000);
n = parseInt(n / 1000, 10);
}
while (arr.length) {
const a = arr.shift();
const x = Math.floor(a / 100);
const y = Math.floor(a / 10) % 10;
const z = a % 10;
str =
(x > 0 ? ones[x] + ' hundred ' : '') +
(y >= 2 ? tens[y] + '-' + ones[z] : ones[10 * y + z]) +
sep[i++] +
str;
}
return str;
};
console.log(sayNumberInEnglish(14)); // returns "fourteen"
console.log(sayNumberInEnglish(1323)); // returns "one thousand three hundred twenty-three"