Skip to content

Instantly share code, notes, and snippets.

@crisu83

crisu83/99-bottles.ts

Last active Jan 3, 2021
Embed
What would you like to do?
My solution for generating the lyrics for the song "99 bottles on the wall" written in TypeScript.
const capitalize = (word: string) => word[0].toUpperCase() + word.substr(1);
const pluralize = (word: string, amount: number) => {
if (amount > 1) return `${amount} ${word}s`;
if (amount === 1) return `${amount} ${word}`;
return `no more ${word}s`;
};
const countBottles = (amount: number) =>
`${pluralize("bottle", amount)} of beer`;
const lyricsForVerse = (i: number, numVerses: number) => {
const numBottles = numVerses - i;
if (numBottles === 0) {
return [
`${countBottles(numBottles)} on the wall, ${countBottles(numBottles)}.`,
`go to the store and buy some more,`,
`${countBottles(numVerses)}...`,
];
}
return [
`${countBottles(numBottles)} on the wall, ${countBottles(numBottles)}.`,
`take ${numBottles > 1 ? "one" : "it"} down, and pass it around,`,
`${countBottles(numBottles - 1)}...`,
];
};
const printLyrics = (numVerses: number) =>
Array.from(Array(numVerses + 1), (_, i) => {
console.log(
lyricsForVerse(i, numVerses).map(capitalize).join("\n") + "\n"
);
});
printLyrics(99);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment