-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottles.html
79 lines (68 loc) · 1.92 KB
/
bottles.html
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
<doctype html>
<html><head>
<title>Check out my new framework, lambda.js</title>
<meta charset="utf-8"></head>
<body>
<div id="output"></div>
<script>
"use strict"
const out = document.getElementById("output")
const print = (msg) => out.innerHTML = out.innerHTML
+ msg.replace(/\n/g, "<br>")
+ "<br>"
const SMALL_NUMBERS = [
"no more",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"]
const LARGE_NUMBERS = [
"",
"",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"]
const countBottles = (n) =>
n == 1 ? "one bottle"
: n < 20 ? SMALL_NUMBERS[n] + " bottles"
: n % 10 == 0 ? LARGE_NUMBERS[n / 10] + " bottles"
: LARGE_NUMBERS[Math.floor(n / 10)]
+ "-"
+ SMALL_NUMBERS[n % 10]
+ " bottles"
const cap = (str) => str[0].toUpperCase() + str.slice(1)
const bottlesVerse = (numberOfBottles, newNumberOfBottles, lastVerse) =>
cap(numberOfBottles) + " of beer on the wall, "
+ numberOfBottles + " of beer.\n"
+ (lastVerse
? "Go to the store and buy some more, "
: "Take one down and pass it around, ")
+ newNumberOfBottles + " of beer on the wall."
const bottlesSong = (i, n) =>
n == undefined ? bottlesSong(i, i)
: i == 0 ? bottlesVerse(countBottles(0), countBottles(n), true)
: bottlesVerse(countBottles(i), countBottles(i-1), false)
+ "\n\n"
+ bottlesSong(i-1, n)
print(bottlesSong(99))
</script></body></html>