11/11 Trouble


#1
var oneFor1 = true
var oneFor2 = true
var oneFor3 = true

for (var i=1; i<2; i++) {
    console.log(oneFor1);
}

while(oneFor2 === true) {
    console.log(oneFor2);
    oneFor2 = false;
}

do {
    console.log("This is ");
} while(oneFor3 === true) {
    console.log(oneFor3);
    oneFor3 = false;
}

The Do/While loop is infinite. Why? Need help.


#2

this the syntax for the do /while loop

do{
//statements
}
while(condition);

So your code must be changed to this

var oneFor1 = true;
var oneFor2 = true;
var oneFor3 = true;

for (var i=1; i<2; i++) {
    console.log(oneFor1);
}

while(oneFor2 === true) {
    console.log(oneFor2);
    oneFor2 = false;
}

do {
    console.log("This is ");
    console.log(oneFor3);
    oneFor3 = false;
} while(oneFor3 === true) ;

Check it out on labs


#3

Thanks for the quick response! :smile: Additional question, on the previous lesson (10/11), I had this as the code and passed:

var once = true    
var getToDaChoppa = function(limit){
      do {
          console.log("Get to da choppa,");
      } while(once === true) {
          console.log("NOW!")
          var once = false
      }
    };
    
    getToDaChoppa(1);

Cause of your help, I now know the correct syntax, but how come in the above instance was this A-Okay? Does this have to do with the limit function and the getToDaChoppa(1);` code? Sorry, if I'm asking for a bit much. I just really want to learn how to do this right.


#4

Ok this one is tricky but I guess it has to do with something called hoisting. As you created a new local variable once by using var once done below you made sure to use this variable inside of the fucntion instead of the global one. And there seems to be a mechanism that first checks for its local variables hoists there declarations to the top. So that you end up with once being undefined or even false when the function begins instead of the global value true. As said this is tricky and not covered in this course just so much it has nothing to do with the function call and if you want to google it the key workd is probably hoisting.