Created
September 2, 2021 16:35
-
-
Save dannydb/de067739972d46a03cceef1b2f6ecdf8 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) { | |
const versesToSing = []; | |
let currentVerse = start; | |
let text = ''; | |
while (currentVerse > end - 1) { | |
versesToSing.push(currentVerse); | |
currentVerse--; | |
} | |
versesToSing.forEach((verseToSing, index) => { | |
const connector = index + 1 < versesToSing.length ? '\n' : ''; | |
text += this.verse(verseToSing) + connector; | |
}); | |
return text; | |
} | |
verse(currentBottles) { | |
const [current, next] = downTo(currentBottles, currentBottles - 1); | |
const nextText = next === 0 ? 'no more' : next; | |
if (currentBottles === 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} bottle${current !== 1 ? 's' : ''} of beer on the wall, ` + | |
`${current} bottle${current !== 1 ? 's' : ''} of beer.\n` + | |
`Take ${current == 1 ? 'it' : 'one'} down and pass it around, ` + | |
`${nextText} bottle${next !== 1 ? 's' : ''} of beer on the wall.\n` | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment