2

(I already know that this isn't the most elegant solution to the 99 bottles code challenge, but I'd really like to know how not to repeat this mistake in the future.)

When this runs in the console it repeats the (count === 0) condition and repeats nothing but the "0 bottles of beer" console log until it crashes.

I've experimented with using a 'break' statement after the count decrements to 0, but haven't had any success.

let count = 99;

function bottlesOfBeer() {
    while (count >= 0) {
        if (count > 0) {
            console.log(count + " bottles of beer on the wall, " + count + " bottles of beer,");
            count--;
            console.log(" take one down, pass it around, " + count + " bottles of beer on the wall.");  
        };

        if (count === 0) {
            console.log(count + " bottles of beer on the wall, " + count + " bottles of beer. Go to the store, buy some more, 99 bottles of beer on the wall.");
        } //*this is where I tried the break statement*
    }
};

bottlesOfBeer();

3 Answers3

4

You only decrement count when it is above 0, so it never goes below 0; but the loop continues as long as count >= 0.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
2

Here's the corrected code:

function bottlesOfBeer() {
  var count = 99;
  while (count > 0) {
    console.log(count + " bottles of beer on the wall, " + count + " bottles of beer,");
    count--;
    console.log(" take one down, pass it around, " + count + " bottles of beer on the wall.");  
  }
  console.log(count + " bottles of beer on the wall, " + count + " bottles of beer. Go to the store, buy some more, 99 bottles of beer on the wall.");
};

bottlesOfBeer();

Please read and understand - and ask, if you have questions.
In the code, count was set to 99.
The while loop stops when count gets to zero.
When the loop exists, count IS zero, and the appropriate line of the song is logged.
I've removed blank lines...
Other than that - your code is pretty neat: no weird indentations (you wouldn't believe what I see - not that it would effect execution, just easier to read).

iAmOren
  • 2,760
  • 2
  • 11
  • 23
  • 2
    Have to admit, I like this more than the suggested edit I put in ScottHunter's answer. eliminates extra comparisons. – TCooper Jun 30 '20 at 00:21
  • 1
    Thanks iAmOren, this explanation is really clear and helped me completely understand where my loop broke down. – ampersandwitch Jun 30 '20 at 00:30
  • @devWitch could you upvote the answer and click the "thank" underneath it - If you so choose? – iAmOren Jun 30 '20 at 00:32
  • Here's the same, with `for` loop: `function bottlesOfBeer() { for(var count=99; count>0; count--) { console.log(count + " bottles of beer on the wall, " + count + " bottles of beer,\ntake one down, pass it around, " + (count-1) + " bottles of beer on the wall."); } console.log("0 bottles of beer on the wall, 0 bottles of beer.\nGo to the store, buy some more, 99 bottles of beer on the wall."); }; bottlesOfBeer();` Added line breaks - `\n`. – iAmOren Jul 01 '20 at 20:11
1

Turn while (count >= 0) into while (count > 0) and you are good to go!

The problem is that when it reaches zero you just log the message without decreasing it any more, so it stays zero and (count >= 0) is always true.

Alekos Dordas
  • 802
  • 7
  • 20
  • 1
    This is my first post and I was worried that the responses to my beginner question would be brutal. I really appreciate your direct answer and friendly tone. Thanks, Alekos. – ampersandwitch Jun 30 '20 at 00:28