Last active
September 5, 2022 07:31
-
-
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.
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
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