I get different results with same input in different challenges

Hello, I am currently trying to solve 4h basic algorithm challenge
(https://www.freecodecamp.org/challenges/find-the-longest-word-in-a-string)
but I got stuck because a “for loop” I tried to use resulted in a single value output while being analogical to this example:

var myArray = [];
for (var x=0; x<5; x++) {
myArray.push(x); }

It returns 5 and in in “Iterate Odd Numbers With a For Loop” challenge
(https://www.freecodecamp.org/challenges/iterate-odd-numbers-with-a-for-loop)
the very same code returns:

[0, 1, 2, 3, 4]

I’ve got no idea why this happens, how should my For Loop look if i wanted it to result in “[0, 1, 2, 3, 4]”?
Sorry for a silly question and thank you for help.

(i) and [i] are different things, meaning in one of the examples, you’re working with the index of the element in the loop, and in the other you’re working with the actual element.

Also, it’s not a silly question at all.

2 Likes

Thanks for an answer!
I still do not fully understand though. :frowning:
Could you perhaps show me how it should look in the other example?

I think I’m a little confused. Which of the challenges are you currently trying to solve?

The algorithm one. I know how to solve it, but i cannot transfer my logic into code because I don’t know how should my For Loop look like if i wanted to get output [0, 1, 2, 3, 4] for example. I do it the way i learned in earlier challenges but as you said “(i) and [i] are different things” - I don’t really comprehend how to refer to the correct element.Pic rel:

I don’t understand why you want the answer from ‘Iterate Odd Numbers With a For Loop’ to be the answer for ‘Find the Longest Words in a String’ because they aren’t the same answers.