Problem with progressing to next task


#1

Hello!

I'm currently at chapter 2. While syntax of JavaScript. Doing this task:

var understand = true;

while(true){
console.log("I'm learning while loops!");
understand = false;
}

When I print something between the while-brackets (here it's "true") and click "Save and Submit Code" the site window freezes. I just can't progress with a new task! The rest of the Codecademy seems to be working fine. I tried re-doing this task several times. Can someone help me or explain what it means? Is it temporary?

BTW I use Chrome browser.

Thanks! :wink:


#2

Looks like you are creating an infinite loop which can freeze a web browser. Try

understand = true;
while(understand == true){
    console.log("I'm learning while loops!");
    understand = false;
}

#3

Thanks very much for the fast reply!
I just tried this before I saw your answer (with 3 equals signs).

var understand = true;

while(understand===true){
console.log("I'm learning while loops!");
understand = false;
}

The console printed:

I'm learning while loops!
false

I'll try your suggestion too.


#4

With this code:

var understand = true;

while(understand===true){
console.log("I'm learning while loops!");
understand = false;
}

You should always receive "I'm learning while loops!" as output. This particular console of codeacademy prints out the false as well for some reason. The code is good.


#5

Maybe it prints "false" to signal that the condition when "understand = false" is met. I thought that maybe the code should be

var understand = true;

while(understand===true){
console.log("I'm learning while loops!");
understand === false;
}

with THREE equals signs in the end part?

I also wonder how my initial attempt

var understand = true;

while(true){
console.log("I'm learning while loops!");
understand = false;
}

could produce an infinite loop. The condition understand = false is still there, isn't it? I'd be grateful if someone explained me.


#6
while(true)

What this essentially says is that while true === true
It will always remain true. But if you put a variable in there to compare if the variable is true or false, you can set that variable to be false somewhere else so you won't get that infinite loop.
Similarly if you just put

var understand = true;
while(understand){
    console.log("I'm learning while loops!");
    understand = false;
}

it will also work since what this line

while(understand){

really means is

while(understand === true){

#7

It's not about the particular console of codecademy, the firefox and chrome webconsoles behave in a similar way. If you go back to the very first exercise where you're asked to enter your name and saw it on the console without console.log this "feature" was used as well. What happens is that when the program terminates with a value in the memory this one is printed. As for example:

understand = false;

has a value of false (you can prove it by console.log(understand = false) ) you see false in the console. If you swap this line with the console.log you won't get an output as console.log uses the value but has no value of it's own (undefined).

@ixentrick About the first version with the === well === is the compare operator that checks if the value and type of the two values on the left and right of it are equal. But you already know that understand is true because you set it to be true in your first line, what you're interested in here is to change it's value from true to false. Because the loop runs if the condition is true but stops when it's false and you want it to stop now (or at least you're told to do so :smile: )

And about the while(true) well let's go through this step by step:

while(true)
--> check if condition is true... condition is literally true  so go on with the stuff in {}
console.log(...)
understand = false;
--> check if condition is true... condition is literally true  so go on with the stuff in {}
console.log(...)
understand = false;
--> check if condition is true... condition is literally true  so go on with the stuff in {}
console.log(...)
understand = false;
--> check if condition is true... condition is literally true  so go on with the stuff in {}
console.log(...)
understand = false;
...

The problem is that the condition doesn't rely on your understand variable so it is false and this value is never changed so it goes on and on and on until your browser refuses to work.


#8

OK, so basically the variable understand is just missing in the condition of the while loop. And that's why the program doesn't react to my attempts to change the value later. :smiley:

Thanks, guys!