10-11 do/while Loops - infinite loop


Stuck at 10-11do/while loops when trying to use with a function (without the function I'm good)
Getting: Infinite Loop and browser crashes

var a=false
var condition=function(a){


The way i understand it is that we defined the parameter to be false, so when we are running the function to yield us "Hey" it should do so once until it hits while(condition) which is false.
Trial & Error: also tried this with condition(true) and var a=false and visa versa--> same problem
Anything to do with the order?


You don't need a function to complete this. Here is what I did:

var loopCondition = false;

do {
	console.log("I'm gonna stop looping 'cause my condition is " + loopCondition + "!");	
} while (loopCondition);

However, I think what you want is something like this:

var a = false;
var condition=function(x){
    do {
    	console.log("I'm gonna stop looping 'cause my condition is " + x + "!");	
    } while(x);



You correctly understand the code. The only problem is the condition of loop. Parameter of function is called a, so it should be:



Ok after @maxgy and @factoradic. I'll give it another try to order this a little:

So let's go through the code step by step:

 var a=false

you start of with a being set to false. Problem here: a is never used again. Sure you use a as the parameter of your function, but a parameter is a local variable of a function so although they share the same name these to variables are in no way connected with each other.

Then you declare a variable condition and assign a function to it and after this you call the function by using:


by using false as the argument, you assign the value of false to the parameter a of your function. So because of this line the value of a inside of your function is false, again that has nothing to do with the var a = false statement that just so happens to have the same name and value (pure coincidence no dependence on each other).

Unfortunately you never use this value as well. So what happens next is that you go into your do/while loop, starting with the console.log part and then the condition is checked:


So what is that condition named condition?
Well the variable condition contains your function and as functions do not occur on the list of falsy values:
they are truthy, meaning that the value in condition is interpreted to being true, meaning you're trapped in an infinite loop.

Hope this makes it easier to understand the other 2 explanations.


Thanks all!
Makes sense! So my thinking behind the useless

var a=false

line was that I was defining a global variable that I could use as a parameter in the function...apparently not.

Also could I still use the following syntax


if I define the condition within the function (a=false)? Probably better to just use while(a) to make it cleaner.

I've changed the code to the below and it works. So here a is defined within the function as false.

var condition=function(a){


With the bottom line I am making it false again, but it doesn't seem necessary.


If your dealing with purely boolean values (true/false) the syntax:


is a perfectly valid abbreviation of bool == true, as you could see by going through the two possible options for bool.

if bool is false, bool == true is false as well
if bool is true, bool == true is true as well

you only get into trouble when comparing non boolean values, in this case you need to have a look at the link.

And about that a=false part here:


You're right the bottom line is not necessary as it is only reached when you finished the loop. So a already needs to be false or at least falsy. And of course in case you start the loop with condition(true) you'll have an infinite loop. So if you want to use it, it should be before or after the console.log in the do{ } part.


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