Count Backwards With a For Loop

Ok, I’ve actually passed this exercise but I’ve come back to it because I don’t feel I fully understand what is happening.

In the for loop the first statement where we set “var i = 10”, wont this reset i to = 10 on each pass.

Why are we ending up with " ourArray = [10,8,6,4,2] " instead of just giving me an array full of 8’s, because we set i to = 10 and then it subtracts 2 each pass?

``````// Example
var ourArray = [];

for (var i = 10; i > 0; i -= 2) {
ourArray.push(i);
}

// Setup
var myArray = [];
for (var i = 9; i > 0; i -= 2) {
myArray.push(i);
}
// Only change code below this line.

``````

Your Browser User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7`.

https://www.freecodecamp.org/challenges/count-backwards-with-a-for-loop

`for(var i = 10; i > 0; i -= 2)`
is similar to saying (pseudo code):

``````var i = 10

loop while i > 0 {
i -= 2;
}
``````

The first argument only run once.
The second argument is used to check at the end of every loop run and also before the first loop run.
The third argument is applied at the end of every loop run.

@brentbyoung,
a for loop statement is actually 4 parts combined into one.

``````for (var i = 10; i > 0; i -= 2)
``````

Is equal to :

``````var i = 10;
while(i > 0) {
//do stuff
i = i - 2;
}
``````

The initialization of i only happens once, before the for loop executes.

Thanks to both of you who replied. This makes much more sense and I can move on now.