Iterate Through an Array with a For Loop

Iterate Through an Array with a For Loop? (Sorry for no explanation.) I’ll be happy if anyone asks me to explain. I’ll love to do it.

Here is the solution:

// Setup

const myArr = [2, 3, 4, 5, 6];

for (let total = 0; total <= myArr.push(myArr.length); total += 20) {

console.log(myArr[total]);

}

console.log(myArr);

// Only change code below this line

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

This is
A) not a good way to use a for loop
B) pure luck that it “works”
C) has variable scope issues

This is not a valid solution unfortunately

Yes, I have a question about how this code works and solves the problem. What’s the back-end computer process?

for (let total = 0; total < myArr.push(myArr.length); total += 20)

Can you please guide me on this one so I know how it works? If I’m thinking wrong, I’m curious about how to solve this using methods like .puch() and .length().
I feel lucky to be here. And feel confident that someone is there to help me.

The code you posted doesn’t work. You need to sum up the contents of the array with code, not by hand.

sorry, i can’t understand, can you please explain it to me?

Your code is wrong.

You shouldn’t type 20 anywhere in your code.

1 Like

Thanks for your time and a really big thanks for your help.

1 Like

Continuing the discussion from freeCodeCamp Challenge Guide: Iterate Through an Array with a For Loop:

Here’s the problem. If you checkout the challenge, it is clearly mentioned that the total variable should be declared and initialized to 0.

which is not followed in this solution. Clarify it. If we discuss the problem, we’ll find a solution. Thanks.

In both of the posted solutions, the variable total is declared and initialized to 0.

but not initialized before condition inside the for loop.

Right. Because the total is not the loop iteration variable. i is the loop iteration variable. You only initialize the loop iteration variable inside of the head of the loop.