Skip to content

Commit dae4a5c

Browse files
committedApr 25, 2017
Beer song solution
1 parent a0ba69c commit dae4a5c

File tree

3 files changed

+418
-0
lines changed

3 files changed

+418
-0
lines changed
 

‎beer-song/README.md

+347
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
# Beer Song
2+
3+
Produce the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
4+
5+
Note that not all verses are identical.
6+
7+
```plain
8+
99 bottles of beer on the wall, 99 bottles of beer.
9+
Take one down and pass it around, 98 bottles of beer on the wall.
10+
11+
98 bottles of beer on the wall, 98 bottles of beer.
12+
Take one down and pass it around, 97 bottles of beer on the wall.
13+
14+
97 bottles of beer on the wall, 97 bottles of beer.
15+
Take one down and pass it around, 96 bottles of beer on the wall.
16+
17+
96 bottles of beer on the wall, 96 bottles of beer.
18+
Take one down and pass it around, 95 bottles of beer on the wall.
19+
20+
95 bottles of beer on the wall, 95 bottles of beer.
21+
Take one down and pass it around, 94 bottles of beer on the wall.
22+
23+
94 bottles of beer on the wall, 94 bottles of beer.
24+
Take one down and pass it around, 93 bottles of beer on the wall.
25+
26+
93 bottles of beer on the wall, 93 bottles of beer.
27+
Take one down and pass it around, 92 bottles of beer on the wall.
28+
29+
92 bottles of beer on the wall, 92 bottles of beer.
30+
Take one down and pass it around, 91 bottles of beer on the wall.
31+
32+
91 bottles of beer on the wall, 91 bottles of beer.
33+
Take one down and pass it around, 90 bottles of beer on the wall.
34+
35+
90 bottles of beer on the wall, 90 bottles of beer.
36+
Take one down and pass it around, 89 bottles of beer on the wall.
37+
38+
89 bottles of beer on the wall, 89 bottles of beer.
39+
Take one down and pass it around, 88 bottles of beer on the wall.
40+
41+
88 bottles of beer on the wall, 88 bottles of beer.
42+
Take one down and pass it around, 87 bottles of beer on the wall.
43+
44+
87 bottles of beer on the wall, 87 bottles of beer.
45+
Take one down and pass it around, 86 bottles of beer on the wall.
46+
47+
86 bottles of beer on the wall, 86 bottles of beer.
48+
Take one down and pass it around, 85 bottles of beer on the wall.
49+
50+
85 bottles of beer on the wall, 85 bottles of beer.
51+
Take one down and pass it around, 84 bottles of beer on the wall.
52+
53+
84 bottles of beer on the wall, 84 bottles of beer.
54+
Take one down and pass it around, 83 bottles of beer on the wall.
55+
56+
83 bottles of beer on the wall, 83 bottles of beer.
57+
Take one down and pass it around, 82 bottles of beer on the wall.
58+
59+
82 bottles of beer on the wall, 82 bottles of beer.
60+
Take one down and pass it around, 81 bottles of beer on the wall.
61+
62+
81 bottles of beer on the wall, 81 bottles of beer.
63+
Take one down and pass it around, 80 bottles of beer on the wall.
64+
65+
80 bottles of beer on the wall, 80 bottles of beer.
66+
Take one down and pass it around, 79 bottles of beer on the wall.
67+
68+
79 bottles of beer on the wall, 79 bottles of beer.
69+
Take one down and pass it around, 78 bottles of beer on the wall.
70+
71+
78 bottles of beer on the wall, 78 bottles of beer.
72+
Take one down and pass it around, 77 bottles of beer on the wall.
73+
74+
77 bottles of beer on the wall, 77 bottles of beer.
75+
Take one down and pass it around, 76 bottles of beer on the wall.
76+
77+
76 bottles of beer on the wall, 76 bottles of beer.
78+
Take one down and pass it around, 75 bottles of beer on the wall.
79+
80+
75 bottles of beer on the wall, 75 bottles of beer.
81+
Take one down and pass it around, 74 bottles of beer on the wall.
82+
83+
74 bottles of beer on the wall, 74 bottles of beer.
84+
Take one down and pass it around, 73 bottles of beer on the wall.
85+
86+
73 bottles of beer on the wall, 73 bottles of beer.
87+
Take one down and pass it around, 72 bottles of beer on the wall.
88+
89+
72 bottles of beer on the wall, 72 bottles of beer.
90+
Take one down and pass it around, 71 bottles of beer on the wall.
91+
92+
71 bottles of beer on the wall, 71 bottles of beer.
93+
Take one down and pass it around, 70 bottles of beer on the wall.
94+
95+
70 bottles of beer on the wall, 70 bottles of beer.
96+
Take one down and pass it around, 69 bottles of beer on the wall.
97+
98+
69 bottles of beer on the wall, 69 bottles of beer.
99+
Take one down and pass it around, 68 bottles of beer on the wall.
100+
101+
68 bottles of beer on the wall, 68 bottles of beer.
102+
Take one down and pass it around, 67 bottles of beer on the wall.
103+
104+
67 bottles of beer on the wall, 67 bottles of beer.
105+
Take one down and pass it around, 66 bottles of beer on the wall.
106+
107+
66 bottles of beer on the wall, 66 bottles of beer.
108+
Take one down and pass it around, 65 bottles of beer on the wall.
109+
110+
65 bottles of beer on the wall, 65 bottles of beer.
111+
Take one down and pass it around, 64 bottles of beer on the wall.
112+
113+
64 bottles of beer on the wall, 64 bottles of beer.
114+
Take one down and pass it around, 63 bottles of beer on the wall.
115+
116+
63 bottles of beer on the wall, 63 bottles of beer.
117+
Take one down and pass it around, 62 bottles of beer on the wall.
118+
119+
62 bottles of beer on the wall, 62 bottles of beer.
120+
Take one down and pass it around, 61 bottles of beer on the wall.
121+
122+
61 bottles of beer on the wall, 61 bottles of beer.
123+
Take one down and pass it around, 60 bottles of beer on the wall.
124+
125+
60 bottles of beer on the wall, 60 bottles of beer.
126+
Take one down and pass it around, 59 bottles of beer on the wall.
127+
128+
59 bottles of beer on the wall, 59 bottles of beer.
129+
Take one down and pass it around, 58 bottles of beer on the wall.
130+
131+
58 bottles of beer on the wall, 58 bottles of beer.
132+
Take one down and pass it around, 57 bottles of beer on the wall.
133+
134+
57 bottles of beer on the wall, 57 bottles of beer.
135+
Take one down and pass it around, 56 bottles of beer on the wall.
136+
137+
56 bottles of beer on the wall, 56 bottles of beer.
138+
Take one down and pass it around, 55 bottles of beer on the wall.
139+
140+
55 bottles of beer on the wall, 55 bottles of beer.
141+
Take one down and pass it around, 54 bottles of beer on the wall.
142+
143+
54 bottles of beer on the wall, 54 bottles of beer.
144+
Take one down and pass it around, 53 bottles of beer on the wall.
145+
146+
53 bottles of beer on the wall, 53 bottles of beer.
147+
Take one down and pass it around, 52 bottles of beer on the wall.
148+
149+
52 bottles of beer on the wall, 52 bottles of beer.
150+
Take one down and pass it around, 51 bottles of beer on the wall.
151+
152+
51 bottles of beer on the wall, 51 bottles of beer.
153+
Take one down and pass it around, 50 bottles of beer on the wall.
154+
155+
50 bottles of beer on the wall, 50 bottles of beer.
156+
Take one down and pass it around, 49 bottles of beer on the wall.
157+
158+
49 bottles of beer on the wall, 49 bottles of beer.
159+
Take one down and pass it around, 48 bottles of beer on the wall.
160+
161+
48 bottles of beer on the wall, 48 bottles of beer.
162+
Take one down and pass it around, 47 bottles of beer on the wall.
163+
164+
47 bottles of beer on the wall, 47 bottles of beer.
165+
Take one down and pass it around, 46 bottles of beer on the wall.
166+
167+
46 bottles of beer on the wall, 46 bottles of beer.
168+
Take one down and pass it around, 45 bottles of beer on the wall.
169+
170+
45 bottles of beer on the wall, 45 bottles of beer.
171+
Take one down and pass it around, 44 bottles of beer on the wall.
172+
173+
44 bottles of beer on the wall, 44 bottles of beer.
174+
Take one down and pass it around, 43 bottles of beer on the wall.
175+
176+
43 bottles of beer on the wall, 43 bottles of beer.
177+
Take one down and pass it around, 42 bottles of beer on the wall.
178+
179+
42 bottles of beer on the wall, 42 bottles of beer.
180+
Take one down and pass it around, 41 bottles of beer on the wall.
181+
182+
41 bottles of beer on the wall, 41 bottles of beer.
183+
Take one down and pass it around, 40 bottles of beer on the wall.
184+
185+
40 bottles of beer on the wall, 40 bottles of beer.
186+
Take one down and pass it around, 39 bottles of beer on the wall.
187+
188+
39 bottles of beer on the wall, 39 bottles of beer.
189+
Take one down and pass it around, 38 bottles of beer on the wall.
190+
191+
38 bottles of beer on the wall, 38 bottles of beer.
192+
Take one down and pass it around, 37 bottles of beer on the wall.
193+
194+
37 bottles of beer on the wall, 37 bottles of beer.
195+
Take one down and pass it around, 36 bottles of beer on the wall.
196+
197+
36 bottles of beer on the wall, 36 bottles of beer.
198+
Take one down and pass it around, 35 bottles of beer on the wall.
199+
200+
35 bottles of beer on the wall, 35 bottles of beer.
201+
Take one down and pass it around, 34 bottles of beer on the wall.
202+
203+
34 bottles of beer on the wall, 34 bottles of beer.
204+
Take one down and pass it around, 33 bottles of beer on the wall.
205+
206+
33 bottles of beer on the wall, 33 bottles of beer.
207+
Take one down and pass it around, 32 bottles of beer on the wall.
208+
209+
32 bottles of beer on the wall, 32 bottles of beer.
210+
Take one down and pass it around, 31 bottles of beer on the wall.
211+
212+
31 bottles of beer on the wall, 31 bottles of beer.
213+
Take one down and pass it around, 30 bottles of beer on the wall.
214+
215+
30 bottles of beer on the wall, 30 bottles of beer.
216+
Take one down and pass it around, 29 bottles of beer on the wall.
217+
218+
29 bottles of beer on the wall, 29 bottles of beer.
219+
Take one down and pass it around, 28 bottles of beer on the wall.
220+
221+
28 bottles of beer on the wall, 28 bottles of beer.
222+
Take one down and pass it around, 27 bottles of beer on the wall.
223+
224+
27 bottles of beer on the wall, 27 bottles of beer.
225+
Take one down and pass it around, 26 bottles of beer on the wall.
226+
227+
26 bottles of beer on the wall, 26 bottles of beer.
228+
Take one down and pass it around, 25 bottles of beer on the wall.
229+
230+
25 bottles of beer on the wall, 25 bottles of beer.
231+
Take one down and pass it around, 24 bottles of beer on the wall.
232+
233+
24 bottles of beer on the wall, 24 bottles of beer.
234+
Take one down and pass it around, 23 bottles of beer on the wall.
235+
236+
23 bottles of beer on the wall, 23 bottles of beer.
237+
Take one down and pass it around, 22 bottles of beer on the wall.
238+
239+
22 bottles of beer on the wall, 22 bottles of beer.
240+
Take one down and pass it around, 21 bottles of beer on the wall.
241+
242+
21 bottles of beer on the wall, 21 bottles of beer.
243+
Take one down and pass it around, 20 bottles of beer on the wall.
244+
245+
20 bottles of beer on the wall, 20 bottles of beer.
246+
Take one down and pass it around, 19 bottles of beer on the wall.
247+
248+
19 bottles of beer on the wall, 19 bottles of beer.
249+
Take one down and pass it around, 18 bottles of beer on the wall.
250+
251+
18 bottles of beer on the wall, 18 bottles of beer.
252+
Take one down and pass it around, 17 bottles of beer on the wall.
253+
254+
17 bottles of beer on the wall, 17 bottles of beer.
255+
Take one down and pass it around, 16 bottles of beer on the wall.
256+
257+
16 bottles of beer on the wall, 16 bottles of beer.
258+
Take one down and pass it around, 15 bottles of beer on the wall.
259+
260+
15 bottles of beer on the wall, 15 bottles of beer.
261+
Take one down and pass it around, 14 bottles of beer on the wall.
262+
263+
14 bottles of beer on the wall, 14 bottles of beer.
264+
Take one down and pass it around, 13 bottles of beer on the wall.
265+
266+
13 bottles of beer on the wall, 13 bottles of beer.
267+
Take one down and pass it around, 12 bottles of beer on the wall.
268+
269+
12 bottles of beer on the wall, 12 bottles of beer.
270+
Take one down and pass it around, 11 bottles of beer on the wall.
271+
272+
11 bottles of beer on the wall, 11 bottles of beer.
273+
Take one down and pass it around, 10 bottles of beer on the wall.
274+
275+
10 bottles of beer on the wall, 10 bottles of beer.
276+
Take one down and pass it around, 9 bottles of beer on the wall.
277+
278+
9 bottles of beer on the wall, 9 bottles of beer.
279+
Take one down and pass it around, 8 bottles of beer on the wall.
280+
281+
8 bottles of beer on the wall, 8 bottles of beer.
282+
Take one down and pass it around, 7 bottles of beer on the wall.
283+
284+
7 bottles of beer on the wall, 7 bottles of beer.
285+
Take one down and pass it around, 6 bottles of beer on the wall.
286+
287+
6 bottles of beer on the wall, 6 bottles of beer.
288+
Take one down and pass it around, 5 bottles of beer on the wall.
289+
290+
5 bottles of beer on the wall, 5 bottles of beer.
291+
Take one down and pass it around, 4 bottles of beer on the wall.
292+
293+
4 bottles of beer on the wall, 4 bottles of beer.
294+
Take one down and pass it around, 3 bottles of beer on the wall.
295+
296+
3 bottles of beer on the wall, 3 bottles of beer.
297+
Take one down and pass it around, 2 bottles of beer on the wall.
298+
299+
2 bottles of beer on the wall, 2 bottles of beer.
300+
Take one down and pass it around, 1 bottle of beer on the wall.
301+
302+
1 bottle of beer on the wall, 1 bottle of beer.
303+
Take it down and pass it around, no more bottles of beer on the wall.
304+
305+
No more bottles of beer on the wall, no more bottles of beer.
306+
Go to the store and buy some more, 99 bottles of beer on the wall.
307+
```
308+
309+
## For bonus points
310+
311+
Did you get the tests passing and the code clean? If you want to, these
312+
are some additional things you could try:
313+
314+
* Remove as much duplication as you possibly can.
315+
* Optimize for readability, even if it means introducing duplication.
316+
* If you've removed all the duplication, do you have a lot of
317+
conditionals? Try replacing the conditionals with polymorphism, if it
318+
applies in this language. How readable is it?
319+
320+
Then please share your thoughts in a comment on the submission. Did this
321+
experiment make the code better? Worse? Did you learn anything from it?
322+
323+
## Setup
324+
325+
Go through the setup instructions for JavaScript to
326+
install the necessary dependencies:
327+
328+
http://exercism.io/languages/javascript
329+
330+
## Making the Test Suite Pass
331+
332+
Execute the tests with:
333+
334+
jasmine-node .
335+
336+
In many test suites all but the first test have been skipped.
337+
338+
Once you get a test passing, you can unskip the next one by
339+
changing `xit` to `it`.
340+
341+
## Source
342+
343+
Learn to Program by Chris Pine [http://pine.fm/LearnToProgram/?Chapter=06](http://pine.fm/LearnToProgram/?Chapter=06)
344+
345+
## Submitting Incomplete Problems
346+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
347+

‎beer-song/beer-song.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const BeerSong = function () {};
2+
3+
BeerSong.prototype.verse = function (nVerse) {
4+
const before = `${nVerse > 0 ? nVerse : 'No more'} bottle${nVerse === 1 ? '' : 's'} of beer on the wall, ${nVerse > 0 ? nVerse : 'no more'} bottle${nVerse === 1 ? '' : 's'} of beer.`;
5+
6+
let action = '';
7+
if (nVerse === 1) {
8+
action = 'Take it down and pass it around';
9+
} else if (nVerse === 0) {
10+
action = 'Go to the store and buy some more';
11+
} else {
12+
action = 'Take one down and pass it around';
13+
}
14+
15+
let after = '';
16+
if (nVerse === 1) {
17+
after = 'no more bottles of beer on the wall.';
18+
} else {
19+
after = `${nVerse > 0 ? nVerse - 1 : 99} bottle${nVerse - 1 === 1 ? '' : 's'} of beer on the wall.`;
20+
}
21+
22+
return `${before}\n${action}, ${after}\n`;
23+
};
24+
25+
BeerSong.prototype.sing = function (start, end = 0) {
26+
let song = '';
27+
28+
for (let i = start; i >= end; i--) {
29+
song += song ? '\n' : '';
30+
song += this.verse(i);
31+
}
32+
33+
return song;
34+
};
35+
36+
module.exports = BeerSong;

‎beer-song/beer-song.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var BeerSong = require('./beer-song');
2+
3+
describe('BeerSong', function() {
4+
var song = new BeerSong();
5+
6+
it('prints an arbitrary verse', function() {
7+
var expected = '8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n';
8+
expect(song.verse(8)).toEqual(expected);
9+
});
10+
11+
it('handles 2 bottles', function() {
12+
var expected = '2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n';
13+
expect(song.verse(2)).toEqual(expected);
14+
});
15+
16+
it('handles 1 bottle', function() {
17+
var expected = '1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n';
18+
expect(song.verse(1)).toEqual(expected);
19+
});
20+
21+
it('handles 0 bottles', function() {
22+
var expected = 'No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n';
23+
expect(song.verse(0)).toEqual(expected);
24+
});
25+
26+
it('sings several verses', function() {
27+
var expected = '8 bottles of beer on the wall, 8 bottles of beer.\nTake one down and pass it around, 7 bottles of beer on the wall.\n\n7 bottles of beer on the wall, 7 bottles of beer.\nTake one down and pass it around, 6 bottles of beer on the wall.\n\n6 bottles of beer on the wall, 6 bottles of beer.\nTake one down and pass it around, 5 bottles of beer on the wall.\n';
28+
expect(song.sing(8, 6)).toEqual(expected);
29+
});
30+
31+
it('sings the rest of the verses', function() {
32+
var expected = '3 bottles of beer on the wall, 3 bottles of beer.\nTake one down and pass it around, 2 bottles of beer on the wall.\n\n2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n\n1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n\nNo more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n';
33+
expect(song.sing(3)).toEqual(expected);
34+
});
35+
});

0 commit comments

Comments
 (0)
Please sign in to comment.