Basic JavaScript - Iterate Through an Array with a For Loop

Tell us what’s happening:

Please help, I don’t know what the issue is. I’ve declared and initialized the total to zero
I used the ‘for’ loop to iterate through ‘myArr’
The total should equal to 20 yet I’m getting the error:

//running tests
total should equal 20.
// tests completed
// console output
ReferenceError: arr is not defined

Your code so far

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

// Only change code below this line

let total = 0;

for (let i = 0; i < arr.length; i++) {

total += myArr[i];

}

Your browser information:

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

Challenge: Basic JavaScript - Iterate Through an Array with a For Loop

Link to the challenge:

There isn’t a variable called arr

1 Like

Welcome to the forum.
If you read the error message you could see whats happening.

You declared a variable called “myArr” but in the for loop you are trying to access “arr” .length

1 Like

Distraction on my part :see_no_evil: Thanks Paolo!

1 Like

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