Loops - Do/While loops


#1

I have trouble with loops a lot and I don't really know why. I have to keep restarting Chrome and I can't figure out why. I think I am finding a bajillion ways to crash a browser.

var thing = true
var getToDaChoppa = function(){
    do {
        console.log("This is gonna give me an infinity loop");
    } while(thing);  
};

getToDaChoppa();

This doesn't work and I don't know what is wrong. Please help!


#2

Firstly, your var thing = true needs a semicolon (;) at the end simply to maintain valid JavaScript. Now, the reason your browser is crashing is because the loop is continuing to loop for the condition while(thing) where thing is true and is never being changed therefore the loop is never stopping. Do you see that?


#3

Yeah. The while(thing) needs to become false then it will stop. right?


#4

That is correct! If you don't do that, it will keep looping and crash the browser.


#5

so what can I do to stop it?


#6

Well, what do the instructions say to do?


#7

Write a do/while loop inside the function we've created for you, getToDaChoppa. The function should log a string of your choice to the console. do it now!
Exact wording


#8

Since it doesn't tell you to do much more than have it run one time (you can simply infer this), you can stop the behavior you're not wanting by initializing thing with false rather than what you currently have it set as (true).


#9

Yes thanks I figured it out. You can also set the while condition to thing = false in the do statement!