While Loops in JS


#1

Guys, ive drawn a blank and cannot for the life of me work it out


var count = 0;
var loop = function(){
while(count=0; count<3; count++:wink:{
console.log("I'm looping!");
}
};

loop();

returns -
SyntaxError: Unexpected token while


#2

So your problem is here. For your while all you need is to get count < 3. You already have count = 0 in your var. However, you do need count++ just after your console.log. So basic outline:

var loop = function(){
    while(count < 3){
        console.log("I'm looping!");
        count++;

So you were very close and had the right stuff just not the right order :smiley: P.S. I assume the smiley face is not part of the code lol


#3

Hey! I think just one part of the problem is that in your while loop, after count ++, you only need the close parenthesis, ), you don't need the semicolon, (which causes the winky face haha!).


#4

Yeah, I thought about that as well. I am not sure since that is not how I went about solving and learning this lesson. Obviously my style is different than his set up but whichever it is should work out well and pass the lesson. Thanks for the back up haha


#5

I can't believe I just noticed this, but you are trying to create a for loop inside of a while loop! Your while loop should just look like while(count === 0) {
and then use your condition, console.log("I'm looping!"), (Don't forget to indent!) and close it the way you did with your curly braces. Does that make sense?


#6

I did not have to make a while(count===0) too pass my lesson? I just had it equal to 0 when var count = 0


#7

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