-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP17.js
More file actions
116 lines (98 loc) · 2.62 KB
/
Copy pathP17.js
File metadata and controls
116 lines (98 loc) · 2.62 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
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
111
112
113
114
115
116
'use strict';
function getWritten(num) {
function getSingleDigit(x) {
switch(x || num) {
case 1:
return 'one';
case 2:
return 'two';
case 3:
return 'three';
case 4:
return 'four';
case 5:
return 'five';
case 6:
return 'six';
case 7:
return 'seven';
case 8:
return 'eight';
case 9:
return 'nine';
break;
}
}
function getDoubleDigit(x) {
var baseDDigits = {
'20': 'twenty',
'30': 'thirty',
'40': 'forty',
'50': 'fifty',
'60': 'sixty',
'70': 'seventy',
'80': 'eighty',
'90': 'ninety'
};
if ((x || num) < 20) {
switch(x || num) {
case 10: return 'ten';
case 11: return 'eleven';
case 12: return 'twelve';
case 13: return 'thirteen';
case 15: return 'fifteen';
default:
var leadingNum = num > 100 ? getSingleDigit(thirdDigit) : getSingleDigit(secondDigit);
if (leadingNum.charAt(leadingNum.length - 1) === 't')
leadingNum = leadingNum.substring(0, leadingNum.length - 1);
return leadingNum + 'teen';
}
} else {
var key = (num > 100 ? secondDigit.toString() : firstDigit.toString()) + '0';
if ((num > 100 && thirdDigit === 0) || secondDigit === 0)
return baseDDigits[key];
return baseDDigits[key] + getSingleDigit((num > 100 ? thirdDigit : secondDigit));
}
}
function getTripleDigit() {
var tripleBase = getSingleDigit(firstDigit) + 'hundred';
if (secondDigit === 0 && thirdDigit === 0)
return getSingleDigit(firstDigit)+'hundred';
else if (secondDigit === 0) {
return tripleBase + 'and'+ getSingleDigit(thirdDigit);
} else {
var tenthMark = +(secondDigit.toString() + thirdDigit.toString());
if (thirdDigit === 0)
return tripleBase + 'and' + getDoubleDigit(tenthMark);
return tripleBase + 'and' + getDoubleDigit(tenthMark);
}
}
var firstDigit = +num.toString().charAt(0);
var secondDigit = +num.toString().charAt(1);
var thirdDigit = +num.toString().charAt(2);
if (num.toString().length === 1)
return getSingleDigit();
else if (num.toString().length === 2)
return getDoubleDigit();
else if (num.toString().length === 3)
return getTripleDigit();
else if (num.toString().length === 4)
return 'onethousand';
}
function convertNumberToWords(max) {
var numbers = [];
for (var i = 1; i <= max; i++) {
numbers.push(getWritten(i));
console.log(i + ': ' + getWritten(i));
}
return numbers;
}
function countLetters(max) {
var words = convertNumberToWords(max).map((word) => {
return word.replace(/\s+g/, '').length;
}).reduce((a, b) => {
return a + b;
}, 0);
console.log(`The length from 1 to ${max} is ${words}`);
}
countLetters(1000);