Solo flight


#1

Hi, can somebody help point out what is wrong with this code?

condition = 1

var soloLoop = function() {
while(condition===1); {
console.log("Looped once!");
condition--;
}
};

soloLoop();


#2

So I changed:

condition = 1 to condition = true
and
condition-- to condition = false

and now it works.

Still wondering why the first version didn't work though?
If 1 = true in code and 0 = false,
then condition =1 should be the same as condition = true
and condition-- would be 1-1=0=false, which is the same as condition = false?

Would appreciate any explanation. Thanks!


#3

hmm... first thing is your condition = 1 needs to be a variable

2nd your condition--; is wrong, you need to change the value of your current variable which is true without the var

so if 1 = true what do you think is false?

hope it helps :wink:


#4

Jep the var is a good point but if you treat 1 like true and 0 as false then using condition-- would be a valid approach to go from true to false. Although it is better to use booleans instead of these I'd call them "implicit values". The real problem seems to be the semicolon here:

while(condition===1);

which makes this line a fully functional while loop. Have a look at this for an explanation what the semicolon does here:


it's about conditions but applies for loops as well.