if (x = 3)
Actually, you assign 3 to x and always return true (not zero). What you need is
if (x === 3)
And you don’t really need the loop here. Just need to check on value
if (value == 3)
Answer from Khiem Tran on Stack Overflow[SOLVED]
In python, you are able to have a while else statement that goes to the else when the while condition is false:
while condition:
# do something
else:
# runs when while is falseIs there any way to do this in JavaScript? Thanks.
Javascript while loop with if/else statements - Stack Overflow
... There is no sense in using a while loop ... More on stackoverflow.com
JavaScript While Loop with if-else if-else and counter - Stack Overflow
Is there any way to do a while-else in JS?
How often do you use while loops
1) You should ensure to declare the variable (var z = 0;) or you will encounter an "undefined variable" error.
2) The while loop condition is wrong, because your loop will never print 110 and 120 with z < 100. You can set while(z < 130) or while(true).
3) I also suggest you to change your break condition from z == 130 to z >= 130, so you're not going into an endless loop in case you decide to change counter increment.
4) Finally, it is a common mistake in while loops to place counter increment in wrong line. In this case when z reaches 100 it will never go on.
var z = 0;
while (z < 130){
z+=10;
if (z==100){
console.log("Cannot print 100!");
continue;
}
else if (z>=130){
console.log("Cannot print after 120!");
break;
}
console.log(z);
}
Because iterator won't to beyong 90 as condition says in while loop. Make that condition z<=120.
Or
while(z <= 120) {
if (z==100) {
// cant print 100 z = z + 10;
continue;
}
// print z
z = z + 10;
}