8. When to "while" and when to "for" (Please help!)


#1

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

I keep getting the message, "Syntax Error: Unexpected Token." I want to understand what I'm doing wrong, but I don't know what to look for. May someone help me understand?

for (var i = 0; i < 5; i++;) {
    console.log(i);
};

var myCondition = true
var myDesire = function(){
    while(myCondition){
        console.log("This finally worked!");
        myCondition = false;
    }
};

#2

for (var i = 0; i < 5; i++; ) { // remove ; [Syntax Error]
console.log(i);
};

and call the function.

myDesire(); //calling the function


#3

This is now my new code, but I'm still receiving the same response. What's wrong with it this time?
for (var i = 0; i < 5; i++:wink: {
console.log(i);
}

var myCondition = true
var myDesire = function(){
while(myCondition){
console.log("This finally worked!");
myCondition = false;
}
};
myDesire(false);


#4

i think you didnt remove ; from there.
for loop should be like ............

for (var i = 0; i < 5; i++ ) {
console.log(i);
}

and here dont need false argument

myDesire(false); // remove false

just call the function

myDesire();


#5

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