forked from turingschool-examples/frontend-mod-1-prework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10_little_monkeys.js
109 lines (91 loc) · 2.94 KB
/
10_little_monkeys.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
/* ## 10 Little Monkeys
Create a file named `10_little_monkeys.js` and within that file, write a program that will print the following nursery rhyme, but for *10* monkeys.
> Ten little monkeys jumping on the bed,
> One fell off and bumped his head,
> Mama called the doctor and the doctor said,
> "No more monkeys jumping on the bed!"
>
> Nine little monkeys jumping on the bed,
> One fell off and bumped his head,
> Mama called the doctor and the doctor said,
> "No more monkeys jumping on the bed!"
>
> Eight little monkey jumping on the bed,
> He fell off and bumped his head,
> Mama called the doctor and the doctor said,
> "Get those monkeys right to bed!"
### Bonus
Can you write the program so that it will run for any number of monkeys?
*/
/*
var monkeys = 5;
for (var i = monkeys; i > 0; i--) {
console.log(i);
}
*/
/*
var monkeys = 5;
for (var i = monkeys; i >= 0; i--) {
console.log(nurseryRhyme);
var nurseryRhyme =
(i + ' little monkeys jumping on the bed\, ' +
'One fell off and bumped his head\, ' +
'Mama called the doctor and the doctor said\, ' +
'"No more monkeys jumping on the bed!" ');
}
*/
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
var teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
function convert_millions(num) {
if (num >= 1000000) {
return convert_millions(Math.floor(num / 1000000)) + " million " + convert_thousands(num % 1000000);
} else {
return convert_thousands(num);
}
}
function convert_thousands(num) {
if (num >= 1000) {
return convert_hundreds(Math.floor(num / 1000)) + " thousand " + convert_hundreds(num % 1000);
} else {
return convert_hundreds(num);
}
}
function convert_hundreds(num) {
if (num > 99) {
return ones[Math.floor(num / 100)] + " hundred " + convert_tens(num % 100);
} else {
return convert_tens(num);
}
}
function convert_tens(num) {
if (num < 10) return ones[num];
else if (num >= 10 && num < 20) return teens[num - 10];
else {
return tens[Math.floor(num / 10)] + " " + ones[num % 10];
}
}
function convert(num) {
if (num == 0) return "zero";
else return convert_millions(num);
}
//end of conversion code
//testing code begins here
function main() {
var monkeys = [10, 9, 8, 7, 6, 5, 4, 3, 2];
var nurseryRhyme =
('little monkeys jumping on the bed\, ' +
'One fell off and bumped his head\, ' +
'Mama called the doctor and the doctor said\, ' +
'"No more monkeys jumping on the bed!" ');
for (var i = 0; i < monkeys.length; i++) {
console.log(convert(monkeys[i]) + ' ' + (nurseryRhyme));
}
var oneMonkey =
('One little monkey jumping on the bed\, ' +
'One fell off and bumped his head\, ' +
'Mama called the doctor and the doctor said\, ' +
'"No more monkeys jumping on the bed!" ');
console.log(oneMonkey);
}
main();