Created
September 2, 2021 16:58
-
-
Save dannydb/32368fab4cf51dd3ebffcd455d6d7dbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { downTo } from './helpers'; | |
export class Bottles { | |
song() { | |
return this.verses(99, 0); | |
} | |
verses(start, end) { | |
return downTo(start, end) | |
.map((i) => this.verse(i)) | |
.join('\n'); | |
} | |
verse(number) { | |
const [current, next] = downTo(number, number - 1); | |
const nextText = next === 0 ? 'no more' : next; | |
const takeText = current === 1 ? 'it' : 'one'; | |
const currentBottles = this.pluralizeBottles(current); | |
const nextBottles = this.pluralizeBottles(next); | |
if (number === 0) { | |
return ( | |
'No more bottles of beer on the wall, ' + | |
'no more bottles of beer.\n' + | |
'Go to the store and buy some more, ' + | |
'99 bottles of beer on the wall.\n' | |
); | |
} | |
return ( | |
`${current} ${currentBottles} of beer on the wall, ` + | |
`${current} ${currentBottles} of beer.\n` + | |
`Take ${takeText} down and pass it around, ` + | |
`${nextText} ${nextBottles} of beer on the wall.\n` | |
); | |
} | |
pluralizeBottles(number) { | |
return `bottle${number === 1 ? '' : 's'}`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment