99 bottles of beer challenge

I am trying to do the 99 Bottles of Beer challenge using a ‘while’ loop and this is what I came up with:

 let beerBottles = 99;
 let beerBottlesAfter = beerBottles--;
 
 while (beerBottles > 0) {
   console.log(
     beerBottles +
       ' bottles of beer on the wall, ' +
       beerBottles +
       ' bottles of beer. Take one down and pass it around, ' +
       beerBottlesAfter +
       ' bottles of beer on the wall.'
   );
 }
 beerBottles--;
 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.'
 );

I’m going through my code step by step, and in my head it works, but it doesn’t work when i actually run it.

Now I understand that this is not the solution, but i can’t seem to understand WHY my code isn’t correct. Can someone help me understand why my code is wrong?

What part is not perfoming as expected?

I spot a few issues with your code.

The main issue is the infinite loop.
You have this condition here

But since you are not updating the value of beerBottles inside the loop, that condition will always be true.

Once you fix that, then the infinite loop will go away.

Your other issue is here

When you use the decrement operator after the variable name like this, it is called the postfix.

The postfix syntax returns the value before it decrements.
So what you are assigning here is actually the value of 99. Not 98 like you think.

You can learn more about the postfix and prefix syntax here

The other issue is that before your loop starts, you are already decrementing the beerBottles values. That means when your prints statements start, it will show 98 bottles instead of 99 like the song traditionally goes.

My advice is to rethink this line of code here completely because it is causing issues.

Once you fix those issues, then the program will run like you expect it to

2 Likes

Thanks for the fast and concise reply!

I tried to fix it by incorporating your feedback, and this is my second version:

let beerBottles = 99;
 
 while (beerBottles > 0) {
   console.log(
     beerBottles-- +
       ' bottles of beer on the wall, ' +
       beerBottles +
       ' bottles of beer. Take one down and pass it around, ' +
       beerBottles +
       ' bottles of beer on the wall.'
   );
 }
 --beerBottles;
 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.'
 );

As you can see, i completely removed
let beerBottlesAfter = beerBottles--;.

I forgot that if you reassign a variable ( in this case the decremented beerBottles, and beerBottles--; is the same as beerBottles = beerBottles-1; ) in any way, that variable is reassigned from that line of code onwards. (sorry if i’m not using the correct terminology btw, still getting used to it :sweat_smile:)

I made the first beerBottles inside the ‘while’ loop have a postfix decrement, so that the result starts decrementing after the first loop only.

Lastly, I made the beerBottles--; outside of the ‘while’ loop to have a prefix decrement instead of a postfix decrement.

Now I ran this new version of my code and I’m happy with the result :slightly_smiling_face: .

Thanks for the feedback, I learned a lot!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.