6/11 Solo flight variable


#1

My completed code:

var myCondition = 0;

var soloLoop = function(){
    while(myCondition === 0) {
        console.log("Looped once!");
    myCondition = false
    }
};

soloLoop();

What I would like to know is, on line 4 why couldn't I use:

while(myCondition) {

*as my Condition = 0 as defined by the variable at the top. This was done with true/false in 4/11 and I am unsure as to why that doesn't apply here.

Thank you :smile:


#2

Well if you have a look at this:
http://www.sitepoint.com/javascript-truthy-falsy/
you can see that 0 used in a condition like this:

while(myCondition) {

would behave falsy meaning that it would be the same as using false and therefore the while loop would not even be able to loop once. 0 and false share even the same value as you can see by 0 == false which is true but they do not have the same type so 0 === false is false.

As you can see myCondition === 0 was true because you used myCondition = 0 and therefore 0===0 was true but false === 0 was false.

Now last but not least the shortcut. If you're only dealing with booleans you just have two options true and false so:

variable === true

must only be tested for 2 options variable = true and variable = false:

variable = true -> true === true  --> true 
variable = false -> false === true  --> false

so as you can see the result is always the same as the input (the value of variable) so it's equivalent to using variable instead. As you have seen from the link the same holds for 0 and 1 but in your case you would have needed to start with 1 :smile: