skip to Main Content

I’m trying of making the lyrics of 99 Bottles of Beer

This is what I’ve got on the last line:

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around,
no more bottle of beer on the wall.

How can I put another line said(With the N capital letter)

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

var beer = 99;
while (beer >= 0) {
  var words = "bottles";
  if (beer === 1) {
    words = "bottle";
  }
  console.log(beer + " " + words + " of beer on the wall, " + beer + " " + words + " of beer.Take one down and pass it around, ");
  beer--;
  if (beer === 1) {
    words = "bottle";
  }
  if (beer === 0) {
    beer = "no more";
  }
  console.log(beer + " " + words + " of beer on the wall.");
}

3

Answers


  1. You can modify your code to add the special case for "No more bottles" after the loop.

    var beer = 99;
    while (beer >= 0) {
      var words = beer === 1 ? "bottle" : "bottles";
      console.log(beer + " " + words + " of beer on the wall, " + beer + " " + words + " of beer. Take one down and pass it around, ");
    
      beer--;
    
      words = beer === 1 ? "bottle" : "bottles";
    
      if (beer === 0) {
        beer = "no more";
        console.log(beer + " " + words + " of beer on the wall.");
      } else {
        console.log(beer + " " + words + " of beer on the wall.");
      }
    }
    
    console.log("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.");
    Login or Signup to reply.
  2. You can also add the console.log under the if loop

    var beer = 99;
    while (beer >= 0) {
      var words = beer === 1 ? "bottle" : "bottles";
      console.log(beer + " " + words + " of beer on the wall, " + beer + " " + words + " of beer. Take one down and pass it around, ");
    
      beer--;
    
      words = beer === 1 ? "bottle" : "bottles";
    
      if (beer === 0) {
        beer = "no more";
        console.log(beer + " " + words + " of beer on the wall.");
        console.log("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.");
      } else {
        console.log(beer + " " + words + " of beer on the wall.");
      }
    }
    Login or Signup to reply.
  3. See the other answers for extensions of your existing code

    Here is a rewrite as DRY as I can get it.

    const plur = (word,count) => count === 0 ? `no more ${word}s` : `${count} ${word}${count === 1 ? "" :  "s"}`;
    const capitalize = str => `${str.slice(0,1).toUpperCase()}${str.slice(1)}`;
    const onTheWall = 'of beer on the wall';
    for (var beer = 99; beer >= 0; beer--) {
        let beerText = plur("bottle",beer);
        let nextBeerText = plur("bottle",beer-1);
        const lines = [
        `${beerText} ${onTheWall}, ${beerText} of beer.`,
         beer > 0 ? `Take one down and pass it around,` : `Go to the store and buy some more,`,
         `${beer > 0 ? nextBeerText : plur("bottle", 99)} ${onTheWall}.`
        ];
        if(beer === 0) lines[0] =  capitalize(lines[0]); // capitalise the "N" on 0 beers
        console.log(lines.join("n"));
    }

    Here is another for fun and education

    const maxBeers = 99;
    const plur = (word, count) => count === 0 ? `no more ${word}s` : `${count} ${word}${count === 1 ? "" : "s"}`;
    const capitalize = str => `${str.slice(0, 1).toUpperCase()}${str.slice(1)}`;
    const onTheWall = 'of beer on the wall';
    
    const song = Array.from({ length: maxBeers + 1 }, (_, beer) => {
        beer = maxBeers - beer; // Reverse the count
        let beerText = plur("bottle", beer);
        let nextBeerText = plur("bottle", beer - 1);
        
        let lines = [
            `${beerText} ${onTheWall}, ${beerText} of beer.`,
            beer > 0 ? `Take one down and pass it around,` : `Go to the store and buy some more,`,
            `${beer > 0 ? nextBeerText : plur("bottle", maxBeers)} ${onTheWall}.`
        ];
    
        if (beer === 0) lines[0] = capitalize(lines[0]); // Capitalize the "N" on 0 beers
        return lines.join("n");
    });
    
    console.log(song.join("n---n"));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search