-
Notifications
You must be signed in to change notification settings - Fork 0
/
P17.js
110 lines (86 loc) · 2.11 KB
/
P17.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function numberLetterCounts(limit) {
let numbers = {
0: '',
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine",
10: "ten",
11: "eleven",
12: "twelve",
13: "thirteen",
14: 'fourteen',
15: "fifteen",
16: "sixteen",
17: "seventeen",
18: "eighteen",
19: "nineteen",
20: "twenty"
}
let tens = {
0: '',
2: "twenty",
3: "thirty",
4: "forty",
5: "fifty",
6: "sixty",
7: "seventy",
8: "eigthy",
9: "ninety"
}
let hundreds = {
1: "hundred"
}
let thousand = {
1 : "thousand"
}
let total = 0, tempStr = "", words = ""
for(let i = 1; i <= limit; i++){
if(i.toString().length == 1 || i <= 20){
console.log(numbers[i])
total += numbers[i].length
}
else if(i <= 99 && i.toString().length == 2){
tempStr = i.toString();
words = tempStr[1] == 0 ? tens[tempStr[0]] : tens[tempStr[0]] + numbers[tempStr[1]]
console.log(words)
total += words.length
}
else if(i.toString().length == 3){
tempStr = i.toString();
if(Number(tempStr.slice(1,)) == 0){
words = numbers[tempStr[0]] + hundreds[1]
console.log(words)
total += words.length
}
else if(Number(tempStr.slice(1,)) <= 9){
words = numbers[tempStr[0]] + hundreds[1] + "and" + numbers[tempStr.slice(2,)]
console.log(words)
total += words.length
}
else if(Number(tempStr.slice(1,)) > 9 && Number(tempStr.slice(1,)) <= 20){
words = numbers[tempStr[0]] + hundreds[1] + "and" + numbers[tempStr.slice(1,)]
console.log(words)
total += words.length
}
else{
words = numbers[tempStr[0]] + hundreds[1] + "and" + tens[tempStr[1]] + numbers[tempStr[2]]
console.log(words)
total += words.length
}
}
else{
tempStr = i.toString();
words = numbers[tempStr[0]] + thousand[1]
total += words.length
}
}
return total
// Good luck!
}
console.log(numberLetterCounts(5));