Skip to content

Instantly share code, notes, and snippets.

@dannydb
Created September 2, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannydb/de067739972d46a03cceef1b2f6ecdf8 to your computer and use it in GitHub Desktop.
Save dannydb/de067739972d46a03cceef1b2f6ecdf8 to your computer and use it in GitHub Desktop.
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