Created
February 1, 2015 21:06
-
-
Save awerlang/ef16cb32ff9605ab8c0d to your computer and use it in GitHub Desktop.
99 bottles of beer, ES6 version
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
function* bottlesOfBeer(howMany=99){ | |
let pluralizeBottles = count => count !== 1 ? "bottles" : "bottle"; | |
let takeNext = next => next >= 1 ? next.toString() : "No more"; | |
for (let i = howMany; i >= 0; i--) { | |
let bottle = pluralizeBottles(i); | |
let bottleNext = pluralizeBottles(i-1); | |
let left = takeNext(i); | |
let toTake = i > 1 ? "one" : (i == 1 ? "it" : "no more"); | |
let toTakeNext = takeNext(i-1); | |
let line1 = `${left} ${bottle} of beer on the wall, ${left.toLowerCase()} ${bottle} of beer.`; | |
let line2 = `\nTake ${toTake} down and pass it around, ${toTakeNext.toLowerCase()} ${bottleNext} of beer on the wall.\n`; | |
yield `${line1}${i > 0 ? line2 : ""}`; | |
} | |
yield `Go to the store and buy some more, ${howMany} bottles of beer on the wall.`; | |
} | |
for (let it of bottlesOfBeer()) { | |
console.log(it); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment