-
Notifications
You must be signed in to change notification settings - Fork 1
/
talking-numbers.js
189 lines (172 loc) · 5.95 KB
/
talking-numbers.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Author: FirstName LastName
let readline = require("readline-sync");
/******************************************************************************
printGreeting()
Prints a simple greeting. Be as creative as you want here. Be sure to include
your name as the author!
*******************************************************************************/
function printGreeting() {
console.log();
console.log("--------------------------------------------------------------");
console.log(" Talking Numbers ");
console.log("--------------------------------------------------------------");
console.log("By: FirstName LastName");
console.log();
}
/******************************************************************************
This function takes a number between 1 and 9 (inclusive) as input and returns
that same number in word form. If the number is not between 1 and 9, the
function returns an empty string, which is just "".
Examples:
sayOneNine(5) → "five"
sayOneNine(8) → "eight"
sayOneNine(13) → ""
*******************************************************************************/
function sayOneNine(num) {
if(num === 1) {
return "one";
} else if(num === 2) {
return "two";
} else if(num === 3) {
return "three";
} else if(num === 4) {
return "four";
} else if(num === 5) {
return "five";
} else if(num === 6) {
return "six";
} else if(num === 7) {
return "seven";
} else if(num === 8) {
return "eight";
} else if(num === 9){
return "nine";
} else {
return "";
}
}
/******************************************************************************
This function takes a number between 10 and 19 (inclusive) as input and returns
that same number in word form. If the number is not between 10 and 19, the
function returns an empty string, which is just "".
Examples:
sayTenNineteen(11) → "eleven"
sayTenNineteen(15) → "fifteeen"
sayTenNineteen(25) → ""
*******************************************************************************/
function sayTenNineteen(num) {
if(num === 10) {
return "ten";
} else if(num === 11) {
return "eleven";
} else if(num === 12) {
return "twelve";
} else if(num === 13) {
return "thirteen";
} else if(num === 14) {
return "fourteen";
} else if(num === 15) {
return "fifteen";
} else if(num === 16) {
return "sixteen";
} else if(num === 17) {
return "seventeen";
} else if(num === 18) {
return "eighteen";
} else if(num === 19){
return "nineteen";
} else {
return "";
}
}
/******************************************************************************
This function takes a number between 2 and 9 (inclusive) as input and returns
that same number in word form, but in the tens place. If the number is not
between 2 and 9, the function returns an empty string, which is just "".
Examples:
sayTwentyNinety(5) → "fifty"
sayTwentyNinety(8) → "eighty"
sayTwentyNinety(1) → ""
*******************************************************************************/
function sayTwentyNinety(num) {
if(num === 2) {
return "twenty";
} else if(num === 3) {
return "thirty";
} else if(num === 4) {
return "forty";
} else if(num === 5) {
return "fifty";
} else if(num === 6) {
return "sixty";
} else if(num === 7) {
return "seventy";
} else if(num === 8) {
return "eighty";
} else if(num === 9) {
return "ninety";
} else {
return "";
}
}
/******************************************************************************
This function calls the three functions above to convert an entire number (num)
into word form, then returns that word. This function should be able to handle
numbers from 0 to 9999 (inclusive).
To do this, first declare a variable named wordForm at the top of the
function. This will be the string you will return at the end of the function,
so initialize it to an empty string at the start: let wordForm = "";
Your first conditional statement should check if num is equal to 0. If so,
set wordForm to the string "zero".
Otherwise, build the word form of num by chopping num into thousands,
hundreds, tens, and ones. Convert each place value number to a word by calling
the three functions above, and concatenate each word with the wordForm variable.
At the very end of your function it should simply return wordForm.
Examples:
sayNumber(0) → "zero"
sayNumber(9999) → "nine-thousand nine-hundred ninety-nine"
sayNumber(75) → "seventy-five"
*******************************************************************************/
function sayNumber(num) {
let wordForm = "";
if(num == 0) {
wordForm = "zero";
} else {
let ones = num % 10;
let tens = Math.floor(num / 10) % 10;
let hundreds = Math.floor(num / 100) % 10;
let thousands = Math.floor(num / 1000);
if(thousands > 0) {
wordForm += sayOneNine(thousands) + "-thousand ";
}
if(hundreds > 0) {
wordForm += sayOneNine(hundreds) + "-hundred ";
}
if(tens === 1) {
wordForm += sayTenNineteen((tens * 10) + ones);
} else {
wordForm += sayTwentyNinety(tens);
}
if(ones > 0 && tens != 1) {
if(tens != 0) {
wordForm += "-";
}
wordForm += sayOneNine(ones);
}
}
return wordForm;
}
/******************************************************************************
This function simply runs your program. At the very least it should greet the
user, ask the user to enter a number between 0 and 9999, then print that
number in word form.
*******************************************************************************/
function run() {
printGreeting();
let number = readline.question("Please enter any number between 0 and 9999: ");
console.log("Word Form: " + sayNumber(number));
console.log("I hope that was correct! Thank you!");
}
// Run the program!
run();
readline.question("Press Enter key to exit.");