Problems with While Loop - Infinite Loop?


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/0/2?curriculum_id=506324b3a7dffd00020bf661

The browser crashes. It seems to be an infinite loop. Although I run the loop and and inside the loop the variable understand is set to false so it shouldn't run again. When I run the code the site stops responding and in bad cases the whole browser freezes and eventually crashes. I have tried diferent conditions, also with numbers (0 and 1) but it still crashes. It even crashes when i set the condition to "poop". When that is the condition it shouldn't run the loop as far as I'm aware.
Help please.


var understand = true;

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


#2

Hi this line

while( understand = true )

you put as condition just understand like that

while( understand)

or you can change the = with == to like that

while( understand == true )

So that the while condiion will be true


#3

I'm not quite sure how you mean but I understood it like this:

var understand == true;

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

It didn't run. It says that ';' is expected.


#4

This line remove the ; at the end

while( understand == true ); <--

#5

I had already tried with and without the ';'
I removed it and tried to run it again, still doesn't work. It still says that a ';' is expected.

var understand == true;

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

	understand == false;
}

#6

okay, lets take a step back. We can use a single equal sign (assign value to variable) or two equal signs (compare things (strings, variables, numbers and so on)

so if we create a variable:

var example = "we create a variable";

or we update a variable:

var example = "now the example variable gets updated";

we use a single equal sign.

If we want to compare (while conditions, if conditions) we use two equal signs:

while (aVariable == "something"){
  console.log("do something");
}

now take another good look at your code, when you should use a single equal sign and when to use two equal signs.

If problems persist, post an updated version of your code


Page becomes unresponsive when trying to submit code
#7

Now I understood it!
thanks for the good explanation.
I was confused because in the "Tutorial" they once said to use "===" and once "=" but I never saw any mention about "==".
I suppose == works the same as === ?

The code works now, thanks.


#8

no, == and === don't work the same. using 3 equal signs also means the data type is compared, for example:

if ("2" == 2)

is true, they are both 2. but:

if ("2" === 2)

is false, since one 2 is a string, the other is an integer.


#9

hope this helps others


#10

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