Skip to content

Instantly share code, notes, and snippets.

@dannydb
Created September 2, 2021 16:58
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/32368fab4cf51dd3ebffcd455d6d7dbe to your computer and use it in GitHub Desktop.
Save dannydb/32368fab4cf51dd3ebffcd455d6d7dbe 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) {
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