Created
July 13, 2020 15:49
-
-
Save aaronmcadam/0105bc66d7288cb1b149897cf5c93345 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 { capitalize, downTo } from "./helpers"; | |
export class Bottles { | |
song() { | |
return this.verses(99, 0); | |
} | |
verses(from, to) { | |
return downTo(from, to) | |
.map((i) => this.verse(i)) | |
.join("\n"); | |
} | |
verse(number) { | |
return capitalize( | |
`${this.firstValue(number)} of beer on the wall, ${this.firstValue( | |
number | |
)} of beer.\n${this.action(number)}, ${this.lastValue( | |
number | |
)} of beer on the wall.\n` | |
); | |
} | |
action(number) { | |
if (number === 1) { | |
return "Take it down and pass it around"; | |
} | |
if (number > 1) { | |
return "Take one down and pass it around"; | |
} | |
return "Go to the store and buy some more"; | |
} | |
firstValue(number) { | |
if (number === 0) { | |
return "no more bottles"; | |
} | |
if (number === 1) { | |
return "1 bottle"; | |
} | |
return `${number} bottles`; | |
} | |
lastValue(number) { | |
if (number === 0) { | |
return "99 bottles"; | |
} | |
if (number - 1 === 0) { | |
return "no more bottles"; | |
} | |
if (number - 1 === 1) { | |
return "1 bottle"; | |
} | |
return `${number - 1} bottles`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment