# freeCodeCamp Challenge Guide: Iterate Odd Numbers With a For Loop

freeCodeCamp Challenge Guide: Iterate Odd Numbers With a For Loop
0
#1

For loops don’t have to iterate one at a time. By changing our `final-expression`, we can count by even numbers.

We’ll start at `i = 0` and loop while `i < 10`. We’ll increment `i` by 2 each loop with `i += 2`.

``````var ourArray = [];

for(var i = 0; i < 10; i += 2) {

ourArray.push(i);

}
``````

ourArray will now contain `[0,2,4,6,8]`

0 Likes

Is it possible that an alternative for loop for the correct answer exists without using <= operator
#2

Solution
to go from counting even number to odd numbers change the count variable initialization from 0 to 1 and the < to <= to include 9 in the array.

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

2 Likes

#3

I tried to find the remainder to determine if it was a odd number, and if it was a odd number the code pushes it to the array.

var myArray = [];

for (var i = 0; i < 10; i++) {
if(i%2 == 1){
myArray.push(i);
}
}

1 Like

#4

Simple code to get exact answer!
var myArray = [];

// Only change code below this line.
for(i=1;i < 10; i+=2){
myArray.push(i);
}
console.log(myArray);

Another option with bit change in code
for(i=1;i <= 9; i+=2)

1 Like

#5

This is how I did it:

// 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);
}

1 Like

#6

Using an if statement.

// Setup
var myArray = [];

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

2 Likes

#7

// Example
var ourArray = [];

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

//It’s my solution
// Setup
var myArray = [];
for (var i = 0; i <= 9; i++){
if(i%2){
myArray.push(i);
}
}
// Only change code below this line.

0 Likes