6/11 - what's wrong please?


#1

//Remember to make your condition true outside the loop!
var myCondition = true;
//var soloLoop = function(){
while(myCondition) {
console.log("Looped once!");
var myCondition = false;
}
};

soloLoop();


#2

Here you have a nice example for shadowing and hoisting if you like google researching stuff. The short version is that by using var before myCondition inside the function you create a second variable with the same name (shadowing), therefore the value of myCondition is not true but undefined when reaching while(myCondition). And why is it undefined instead of at least false? Well that is called hoisting when the declaration of a local variable is hoisted to the top of the function (meaning before anything else it implicitly does something like var myCondition). The best workaround that I could think of is to move the var myCondition = true inside the function before the while loop so you have only 1 variable that is always set to true when the function is run and changed when the loop is run (of course get rid of the var inside the loop then). Hope this helps although it was pretty fast for maybe not so easy topics.