Issues with 'introduction to 'while' loops in JS 11. review


#1

hey guys I am REALLY confused with this I literally copied and pasted the "stuck? get a hint!" syntax for the variable "condition" and it still says "SyintaxError: Unexpected token var" :rage:
and I have never had an issue with variables till now here is my code

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

    do 
    
    {console.log('do loop:');}
   
  var condition = true; //I'm pretty sure this is the variable it is talking about unless
// if it is the variable inside my for loop

while(condition = true) {

    console.log("logged");
    
    condition = false;
} // please help!

#2

Oh yes you end the {} but before the while you have this line:

var condition = true; //I'm pretty sure this is the variable it is talking about unless

that should not be there and as the interpreter normally lazy evaluates (stops at the first error) it complains about the unexpected var.

Also keep an eye on this condition:

while(condition = true) {

that is pure evil and creates an infinite loop :wink:

hint

= assignment
== compare values
=== compare values and types


#3

ohh ok thanks! lol that '=' was unintentional :blush:


#4

ok now the do loop is causing an infinite loop somehow here's my code. when I remove the do loop it runs fine

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

var condition = true

do {
console.log("I'm logged...");
} while(condition===true){console.log('need to put some random code here')
condition = false
};

and if you can cause an infinite loop with a do loop plz tell me how it happens so I can avoid it


#5

Have a look at this one: