Skip to content

Instantly share code, notes, and snippets.

@crisu83
Last active September 5, 2022 07:31
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 crisu83/a89b91119a2016104f1bc14fde6ccbf9 to your computer and use it in GitHub Desktop.
Save crisu83/a89b91119a2016104f1bc14fde6ccbf9 to your computer and use it in GitHub Desktop.
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;
return numBottles === 0
? [
`${countBottles(numBottles)} on the wall, ${countBottles(numBottles)}.`,
`go to the store and buy some more,`,
`${countBottles(numVerses)}...`,
]
: [
`${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