Skip to content

Instantly share code, notes, and snippets.

@tgaeta
Last active September 17, 2023 01:39
Show Gist options
  • Save tgaeta/5252c4b1b3b2a495f4369d3bc5a00072 to your computer and use it in GitHub Desktop.
Save tgaeta/5252c4b1b3b2a495f4369d3bc5a00072 to your computer and use it in GitHub Desktop.
99bottles.ts
// TS Playground: https://www.typescriptlang.org/play?#code/MYGwhgzhAEBCD2AXRICmMDeAoa0LwDsBzACgEoAuPRAJwEtjptdcbVEBXGg6RACzoQAdADdUNCOhIBOaQBpoABjIBuHNAC+WdWImoSBKgQ4BbAEbjK1eo2Ys2nbtAAGAEgz9BQiIjA1EBmQa0O6ewgAOIFxgIHQAXmCIdISBwfAAZtAW4tCEvHyo0ADuMSAKztAA1OosLqECwj5+AQRkQojwADLwReIAwpD6QSEeDUKR0bEJSSmtaZnZNEIAOgQVldA1LGFCiwD6wHzwbIFquFo64pIQJOk08CZGphY0Ch1P5pZUPjZETFtoRDQXTXACCNBo31oDCIAG0ALrQAC80ARZxY6WOJEB0DoyOgdweKlx0AAfCiOsS6ABaalkf61XAg9DgpbhDgQPgkHbMkh0MiqLZaWoOLg8ZkQVlCABW8AYJAARKsFYLztpcBMaDF4olkgQDB8XlYfjCGfZ2GLoDwkTboABGaAAfmgAHIzEgUKgXdAqG6PWgIC70RcNfcCPAOPrDFbnl9rKa7KwLU5rbaHc6XXREN7fYQvcH1dA2CYwAwYQaY58aFZjFXoAAfeO2LaipwGaDU+30m0oxRO13h6AmY5en1Wjv2gvqJr+Cu1o1Q35mpOOHip3v9l0AOXgQ5HOat7XgAGVocRyFPcPtDiO57Hq4uE1s6JluWNi6WCOXWsjbdS7fSibmqu0AKgA4ruHT5IUPgjtAYAEAAJlkHAAJ54A8hTDmwCiyFk-roLkCyoDkeT8IUJQgCAKwEAq6LnNAqAgJIy7AZazgACpgAA1oU9ReOEYYRlGwyIT0PAIch4SQDAWbwfckaIQo-HCB+ZbnnMLhVFstRuKMAlRFqUy6rME4AfMWQkTQuQ8ORxSlDRzj0Zo6haBcwCED4+HIAG+IEKgRRwARNyCh5BD4GgQggPApDuj56DeIQpACmoWAAPRpdAYURagUUxSQcWesIvIAKwpdAGXQBxDTFHQVHQEQdBiNAqERtBwJXIUmLWSV3lFUI6WZdl8CRdFsXBaInU3AAzAoAGqBVmXVYItX1Y1zWtRwHV6DA3XQNNfW+VBDqFZFWBAA
class Bottles {
song(): string {
return this.verses(99, 0);
}
verse(n: number): string {
return `${this.start(n)} ${this.pluralization(n)} of beer on the wall, ` +
`${this.start(n).toLowerCase()} ${this.pluralization(n)} of beer.\n` +
this.beer_chore(n);
}
verses(from: number, to: number): string {
let versesArr: string[] = [];
for(let i = from; i >= to; i--) {
versesArr.push(this.verse(i));
}
return versesArr.join("\n");
}
pluralization(n: number): string {
return n === 1 ? 'bottle' : 'bottles';
}
pronoun(n: number): string {
return n === 1 ? 'it' : 'one';
}
remaining(n: number): number | string {
return (n - 1) === 0 ? 'no more' : n - 1;
}
start(n: number): string {
return n === 0 ? 'No more' : n.toString();
}
beer_chore(n: number): string {
if (this.remaining(n) === -1) {
return "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
} else {
return `Take ${this.pronoun(n)} down and pass it around, ${this.remaining(n)} ` +
`${this.pluralization(n - 1)} of beer on the wall.\n`;
}
}
}
const bottles = new Bottles();
console.log(bottles.song());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment