8. When to 'while' and when to 'for' Infinite loop?


#1



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


Hi, For this exercise I used this code, and it is killing the page. I don't think I logged an infinite loop, could someone tell me if I did? Thanks!


var understand = true;

while (understand =true){
	console.log("I like hotdogs!");
	understand = false;
}


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


#2

Re-read the first line:

while (understand =true){

The condition you wrote above is an assignment expression. Here, the variable understand is assigned a value of true. Also note that an assignment expression gives the value that was being assigned. So, understand =true gives true. And thus the while loop remains true and it becomes an infinte loop.

Do you know how to fix the problem? :wink:


#3

I'm not sure if this was the best way to fix it, but I changed

while (understand =true){

to this

while (understand =false){

And it went through, is that what I should have done?


#4

You need to use the equality test operator == :wink:


#5

Oh, ok :slight_smile:, thanks for your help!