Iterate Odd Numbers With a For Loop Help Understanding

Iterate Odd Numbers With a For Loop Help Understanding
0

#1

Tell us what’s happening:
Hi,

I passed the challenge but I am looking for some explanation to understand the code better.

What really confused me was the odd numbers. My first attempt was to do an i+= 3 but that narrowed it too much. Then I tried 2 and it worked but I don’t know why. Please could someone explain to me what is going on and why it works.

Your code so far

// Example
var ourArray = [];

for (var i = 0; i < 10; i += 2) {
  ourArray.push(i);
}

// Setup
var myArray = [];

// Only change code below this line.

for (var i = 1; i <= 9; i += 2) {
   myArray.push(i);
}

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6.

Link to the challenge:


#2

If you start at the number 1 and keep adding 2 each time, each new number will always be odd. The sequence would be 1, 3, 5, 7, 9,…