-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path017-number-letter-counts.js
59 lines (49 loc) · 2.3 KB
/
017-number-letter-counts.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
/* Object Contains String Representation of Numbers in English */
let strings = {
'units' : [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine',
'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'],
'tens' : ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
}
let numberLetterCounts = (limit) => {
let sum = 0
let i
/* Go through every number from 1 to limit */
for(i = 1; i <= limit; i++){
let currentNumber = i
let numberString = ''
/* Calculate how many thousands current number contains, add to number string, and
then subtract to have hundreds only */
let thousands = Math.floor(currentNumber / 1000)
if(thousands > 0){
numberString = numberString + strings['units'][thousands] + 'thousand'
currentNumber = currentNumber - (thousands * 1000)
}
/* Calculate how many hundreds current number contains, add to number string, and
then subtract to have tens only */
let hundreds = Math.floor(currentNumber / 100)
if(hundreds > 0){
numberString = numberString + strings['units'][hundreds] + 'hundred'
currentNumber = currentNumber - (hundreds * 100)
if(currentNumber > 0){
// We don't need the 'and' if there's nothing left
numberString = numberString + 'and'
}
}
/* If remaining is less than twenty, add the appropriate string */
if(currentNumber < 20){
numberString = numberString + strings['units'][currentNumber]
}else{
/* If remaining is more than twenty, split into tens and units and add strings */
let tens = Math.floor(currentNumber / 10)
numberString = numberString + strings['tens'][tens]
currentNumber = currentNumber - (tens * 10)
numberString = numberString + strings['units'][currentNumber]
}
/* Calculate length of current number's string representation and add to sum */
console.log(i + ' : ' + numberString)
sum = sum + numberString.length
}
return sum
}
/* Check Solution */
console.log('Result is ' + numberLetterCounts(1000))