Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement