Iterate Odd Numbers With a For Loop1

Tell us what’s happening:
i need someone to explain to me the process of
+=or-= and to help my code pass the test

Your code so far


// Example
var ourArray = [];

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

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


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:65.0) Gecko/20100101 Firefox/65.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-odd-numbers-with-a-for-loop/

i += 1 means i = i + 1, and it is raising the value of i by 1 at each loop iteration

Change 1 with 2 and you have the explanation of the other one

you are telling me that if i=2, then i+=2 will add and i=(2) +2 which is 4?

Yes, as you start with i having a value of 0 (var i =0) then it will do 0, 2, 4 etc

You should also respect this, but you didn’t - it may make tests fail even if you did everything else correctly

okay i get it. Means the solution for the exercise is

for (var i = 1; i <= 9; i += 2)

ryt??

odd numbers are those that are not divisible by 2 at least their division by 2 does not give a whole number and the operation + = allows to add the number to the right of the operator to the current value of the variable of left which means that if you have var A = 5; A += 2 you will add 2 to the actual value of A so a will be worth 7 and if A += 3 you will add 3 to the actual value of A so A will be worth 8 and so on.
So if you want to iterate through odd number from 1 to 9 you have to init you loop var to 1 and iterate untill 9 which means x <= 9 or x < 10 and for the incrementation i let you guess

1 Like

What does it say if you use that code?

i passed the test with the codes

1 Like

i will use the incrementation i+=2 since i=1 am i correct?

exactly do it and try to pass the test

1 Like

// Only change code below this line.
:frowning:
for(var i = 0; i < 10; i+=1) {
if(i%2==0){
continue;
}
myArray.push(i);
}