2. While Syntax


#1

My webpage seemed to freeze multiple times when I clicked submit code. I tried this on firefox too, and it had the same problem.

var understand = true;

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

I didn't change line 5 like it said not to. When I clicked submit code, it showed the button being pressed and the cursor just turned into a hand and froze


#2

Your webpage keeps freezing because you threw an Infinite loop into your solution. 5 will always be less than 8 NO MATTER WHAT. so if the statement is always true. It will continuously create it. Loops will continue to go on until theirjob is completed.


#3

but didnt it later state understand is false to stop the loop?


#4

You must set the condition to "understand". Otherwise you will simply be putting in a true condition that stays true. If your condition is, for instance, 5<8, that is true, and setting the variable "understand" to be false does not alter the validity of the condition of 5<8.


#5

You beat me to it.. Why D;


#6

continued -- since setting "understand" to false does not change the argument to false, the while loop will continue infinitely, thus overloading your browser, which is why it's freezing.


#7

Sorry, my bad. I'll delete my reply if you'd like to take over.


#8

No it's okay. As long as they understand it and get end results you're fine.


#9

Should they have any more questions I will leave them up to you.


#10

No no no you got it :smiley: I promise


#11

Thanks all for the help.


#12

While loop: here is my code"
var count =0;

var loop = function(phrase){
while (count++ < 3){
console.log(phrase);
}
};

loop("I'm looping");

I get the 3 "I'm looping" phrases printed out, BUT, I get this message -- "Oops, try again. It looks like you're not printing the right string to the console." -- and can't move on. What is the error?

LO26


#13

Get rid of phrase in var loop = function(), get rid of the ++ in count++ < 3. make the console.log phrase to actually say the phrase I'm looping! and instead of having count++ in right after the while change to count++ to be after the console.log


#14

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