-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path99-bottles.js
More file actions
70 lines (61 loc) · 1.94 KB
/
99-bottles.js
File metadata and controls
70 lines (61 loc) · 1.94 KB
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
var _ = require('../lib/underscore');
// naive
function pluralize(word, count) {
return (count === 1) ? word : word + 's';
}
function pluralizer(singular, plural) {
return function(count) {
count = Math.abs(count);
return (count == 0 || count > 1) ? plural : singular;
}
}
var cats = pluralizer('cat', 'cats');
console.log(cats(1)); //=> cat
console.log(cats(-1)); //=> cat
console.log(cats(5)); //=> cats
console.log(cats(0)); //=> cats
console.log(cats(-3)); //=> cats
function lyricSegment(n) {
var bottles = function(count) {
if (count > 0) {
return count + ' ' + pluralizer('bottle', 'bottles')(count);
} else {
return 'No more bottles';
}
}
return _.chain([])
.push(bottles(n) + ' of beer on the wall')
.push(bottles(n) + ' of beer')
.push('Take one down, pass it around')
.push(bottles(n - 1) + ' of beer on the wall')
.value();
}
console.log(lyricSegment(5));
//=> [ '5 bottles of beer on the wall',
//=> '5 bottles of beer',
//=> 'Take one down, pass it around',
//=> '4 bottles of beer on the wall' ]
console.log(lyricSegment(1));
//=> [ '1 bottle of beer on the wall',
//=> '1 bottle of beer',
//=> 'Take one down, pass it around',
//=> 'No more bottles of beer on the wall' ]
function songLyrics(start, end, lyricGen) {
return _.reduce(_.range(start, end, -1), function(lyrics, n) {
return lyrics.concat(lyricGen(n));
}, []);
}
var lyrics = songLyrics(3, 0, lyricSegment);
console.log(lyrics);
//=> [ '3 bottles of beer on the wall',
//=> '3 bottles of beer',
//=> 'Take one down, pass it around',
//=> '2 bottles of beer on the wall',
//=> '2 bottles of beer on the wall',
//=> '2 bottles of beer',
//=> 'Take one down, pass it around',
//=> '1 bottle of beer on the wall',
//=> '1 bottle of beer on the wall',
//=> '1 bottle of beer',
//=> 'Take one down, pass it around',
//=> 'No more bottles of beer on the wall' ]