3. console crushing when i try to use while loop


#1

hey,
I tried solving exercise 3 using a while loop and the console crushes. any idea why? when i did it with a "for" loop it worked but i can't figure out why "while" didn't work. here is my syntax:

var count = 1;
while (count < 21){
if (count%3===0 && count%5===0){
console.log("FizzBuzz");
}
else if(count%3===0 && count%5!==0){
console.log("Fizz");
}
else if(count%5===0 && count%3!==0){
console.log ("Buzz");
}
else{
console.log(count)
};
var count = count++;
};

thanks!


#2

Hi @itaydafna

You have to change

var count = count++;

to

count++;

and it works.

The reason it's because you create a new variable count, but the value count++ is "NaN" (NotANumber) so your while loop is going to be infinite.

Hold on :wink:


#3

var count = count++; is not a valid statement. It's like saying var count = count = count + 1. Using count++ by itself is sufficient.


#4

Here is my code:

for (var i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 3) {
console.log ("Fizz");
} else if (i % 5) {
console.log ("Buzz");
} else {
console.log (i);
}
}

I'm really confused, can someone help explain why this isn't working. Thank you!


#5

got it!,
thanks man! :smile:


#6

@letsgohyde You forgotted the === 0 part in the other else ifs