Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. function* bottlesOfBeer(howMany=99){
  2. let pluralizeBottles = count => count !== 1 ? "bottles" : "bottle";
  3. let takeNext = next => next >= 1 ? next.toString() : "No more";
  4. for (let i = howMany; i >= 0; i--) {
  5. let bottle = pluralizeBottles(i);
  6. let bottleNext = pluralizeBottles(i-1);
  7. let left = takeNext(i);
  8. let toTake = i > 1 ? "one" : (i == 1 ? "it" : "no more");
  9. let toTakeNext = takeNext(i-1);
  10. let line1 = `${left} ${bottle} of beer on the wall, ${left.toLowerCase()} ${bottle} of beer.`;
  11. let line2 = `\nTake ${toTake} down and pass it around, ${toTakeNext.toLowerCase()} ${bottleNext} of beer on the wall.\n`;
  12. yield `${line1}${i > 0 ? line2 : ""}`;
  13. }
  14. yield `Go to the store and buy some more, ${howMany} bottles of beer on the wall.`;
  15. }
  16.  
  17. for (let it of bottlesOfBeer()) {
  18. console.log(it);
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement