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:

What is the name of the challenge you are currently trying to solve?

Can you copy/paste your current code into a reply? Also, please do not use screenshots, because it is easier for us if we have the actual code to test.

To enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

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.