6. Solo Flight


#1



Whenever I go to run my code and click that save and submit code, my computer curser starts doing that loading thing and then I can't click on anything and the code never runs. I have to refresh my page and try again but nothing works.

Here is my code:


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

soloLoop();

please help me with what is happening or what I am doing wrong


#2

Hi this line remove the ;

while (myCondition); <---

#3

Well @wizmarco already said how to fix this, so here is just a short explanation what is happening.

my computer curser starts doing that loading thing and then I can't click on anything and the code never runs.

First of, this is a strong indicator of an infinite loop. Which means that you go through the same steps over and over again. Now the browser might think it is just to slow and reaches for more resources to fix this issue but as you're just stuck in a loop this will lead to a freeze and a message asking you to stop (in good browsers).

Now the problem is the semicolon and to explain this one need to know that the {} after conditions and loops aren't mandatory but optional, it is just that in those cases only the very next statement is taken into account. For example

if(condition)
    statement1;
    statement2;

statement1 would be connected to the condition and statement2 would be executed no matter what. So now ; is the most basic statement meaning "Do nothing at all PERIOD" where the semicolon is somewhat the period of the sentence. This means your

while (myCondition);

translates to:

while(true){
 // nothing to do here
}

and once your done with this part (which you never will as the condition is always and will always be literally true) you could execute this part:

{
    console.log("Looped once!");
        myCondition = false;
  }

where the {} just indicate that this whole code inside is wrapped to one statement (that is the reason it works with loops and conditions in the first place :slight_smile: )

Hoe this makes some sense to you.
Good Luck