What am I doing wrong in this loop


#1

for (var i = 100; i < 1; i = i- 1) {
console.log(i);
}


#2

It will never run because your condition doesn't ever count as true.

Change the "<" sign to ">" and that should help!


#3

var i =100;
for(; i >= 1; i--) {
console.log(i);
}


#4

Thanks that helped alot!


#5

@tagmaster, the "var I = 100;" has to be in the for loop for the loop to work.

eg.

for(var i = 100 ; i >= 1 ; i--) { //(Initialization ; Condition ; Increment || Decrement)
    console.log(i);
}

#6

That's acutally ok, as shown here:

you could even write:

    var i=100;
    for(;;){
        if(!(i>=1)){
           break;
        }
        console.log(i)
        i--;
    }

the only question is: Why should you do this? The main advantage of a for loop is that you can write it this compact and not have to spread the important parts all over your program.