Basic JavaScript - Count Backwards With a For Loop

Tell us what’s happening:
I copy the code in the example but the result in my console is different based on the example.

// console output
1
2
3
4
5

Your code so far

const ourArray = [];

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Count Backwards With a For Loop

Link to the challenge:

the output here is the result of the push function
so you will see the size of the array being printed out
1 when you have one thing
2 when you have 2 things in the array etc.

this is my output in odd
const myArray = [ ];

for (let i = 9; i > 0; i -= 2) {
console.log( myArray.push(i));
}

// running tests
// tests completed
// console output
1
2
3
4
5

What is in your arry? Is it what is required?

Push the odd numbers from 9 through 1 to myArray using a for loop.

But the console output is 1,2,3,4,5. It is not outputing 9, 7, 5, 3, 1

you are not logging the array…
you are logging the output of the push which is just the length of the array

ah I see
I get it now

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.