Stuck on Iterate Through an Array with a For Loop

So I got the hang of the other loop lessons, but I’m stuck on this one.

My error messages are

total should equal 20

I’m not sure how to get total to equal 20. Making i = 20 doesn’t work so I’m stumped.

You should use a for loop to iterate through myArr

I thought that was what I doing here, so I’m not sure why my for loop isn’t a for loop.

// Setup
const myArr = [2, 3, 4, 5, 6];
for (let i = 0; i < myArr.length; i++){
let total = 0;
total += myArr;
}
// Only change code below this line

  **Your browser information:**

User Agent is: Mozilla/5.0 (iPad; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/102.0 Mobile/15E148 Safari/605.1.15

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

Can someone explain why adding total +=myArr[i]; make the for loop work properly now? Is this something to do with bracket notation?

// Setup
const myArr = [2, 3, 4, 5, 6];
for (let i = 0; i < myArr.length; i++){
let total = 0;
total += myArr[i];
}
// Only change code below this line

If you’ve forgotten about accessing values inside arrays check out this lesson:

myArray is an array with 5 values inside.

console.log(myArray);    // [2, 3, 4, 5, 6]
console.log(myArray[0]); // 2
console.log(myArray[1]); // 3
console.log(myArray[2]); // 4
console.log(myArray[3]); // 5
console.log(myArray[4]); // 6

The numbers we use to access a certain value are called an “index”. Notice the “0” index refers to the first value in the array, the “1” index refers to the second value, and so on.


Back to the lesson you’re asking about, it explains that a common task is to “loop through” an array, accessing each of its values in turn. In this lesson we want to use that technique to calculate the total of all the numbers in myArray.

Here’s the starter code:

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

// Only change code below this line

And here’s the instructions:

Declare and initialize a variable total to 0. Use a for loop to add the value of each element of the myArr array to total.

So, here are the steps:

  1. declare a variable named total and assign it the value 0
  2. using a for loop, access each value in myArray in turn, and add it to total

Notice, declaring total needs to happen before the for loop.

1 Like

You cannot declare total inside the for loop, put it before.

1 Like

…I just realised the numbers in myArr when added equal 20.

The whole thing makes sense now, I was confused where the 20 came from and forgot to try doing basic maths. It makes sense to add the numbers in the array to total to get total to equal 20.

Okay solved it! Thanks!

Maybe the test should read

total should equal 20 (the sum of the numbers in myArray)

Would that be more understandable?

1 Like

For me personally, yes. Like I said, I could have added the numbers in the array and worked it out myself if I’d thought to.

But that detail would have made me realise where the 20 came from far more quickly.