6. (While loop) solo flight


#1


I'm very very confuse with this practice :frowning: HELP!!!


(syntax error.)


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

soloLoop();


#2

The basic problem you have is you are missing a closing curly brace "}" for you while loop. Also your code is poorly formatted. I would change it so it looks more like this:

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

};

soloLoop();

#3

that's great. thank you. it worked now.


#4

As @mkordik already said, the problem is a lack of curly braces.
Just some additional information about what is happening.
The problem here is that it actually works to omit the curly braces, but in this case the keyword (while,for,if,else,...) only reaches on to the very next statement. Which leads to this:

var condition = true;
...
while (condition) { // just to illustrate the scope
    console.log("Looped once!"); 
} // just to illustrate the scope

which is obviously an infinite loop and which prevents you from reaching the statement that sets the condition to be false.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.