While (infinite) run infinitely


#1


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


This code seems to be creating and infinite loop, though I do not understand why.


var i = true;

var soloLoop = function(){           //enter the loop
   while(true){                             //i is true so we continue
   console.log("Looped once!");   //should print on screen "Looped once!"
   i = false;                                //i is now false.  This should stop the loop.
   }
};

soloLoop();                                //This calls the function which should result 
                                                //in "Looped once!"

results in an infinite loop and the browser giving up waiting to regain control


#2

You didn't say while what was true. One way to say while value is true would be to just say the value, for instance:

while(i)

That is one way to say while i is true then print this code.

Hope this helped :slight_smile:


#3

The problem is occurring in your while loop's conditional statement. Instead of looking for true you should be looking for while i is true. Use while(i) and you should be good.


#4

Ah. yes, that resolves it. Thank you, both. I appreciate the help.


#5

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