When to "while" and when to "for"


#1

I'm getting it, albeit at a slow pace. I just can't seem to figure out where my syntax errors come from any time I attempt something from scratch.


var condition = true
var myLoop = 0

while (condition) {
    console.log("Happy life!")
    var condition = false
    for (myLoop < 3) {
        myLoop++;
        console.log("That's great!")
    }
}


#2

a for loop has 3 parts:

for ([initialization]; [condition]; [final-expression])

yours only has the condition.


#3

Thanks! I knew I was missing something...

My familiarity:

for (var i = 0; i < 5; i++) {
????
}

This is about as far as I've come. I've seen a lot more. I guess I just don't understand it yet.


#4

that looks a lot better :slight_smile:


#5

Fixed!!

var condition = true
var myLoop = 0

while (condition) {
    console.log("Happy life!")
    var condition = false
    for (var i = 0; i < 5; i++) {
        console.log("That's great!")
    }
}

#6

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