Basic JavaScript - Iterate Through an Array with a For Loop

Tell us what’s happening:
Describe your issue in detail here.
Cant seem to figure out why my code keeps giving me 0 instead of 20. Already watch a couple of videos and read a couple articles , not sure how to fix it.

  **Your code so far**
// Setup
const myArr = [2, 3, 4, 5, 6];

// Only change code below this line
var total = 0;
for (var i = 0; i < myArr.lenght; i++) {
total += myArr[i];
}
console.log(total);
  **Your browser information:**

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

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

Link to the challenge:

Typo.

Note that you should not be using var

Even after changing the var for let it keep giving me this error:
" // running tests

total

should equal 20. // tests completed"

Did you fix the typo?

This is my current code:

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

// Only change code below this line
let total = 0;

for (let i = 0; i < myArr.lenght; i++) {
total += myArr[i];
}

You still have this typo.

I’m sorry, I don’t if I just have been staring at the screen for too long but it worked “length” not “lenght” smh.

Thank you so much!

1 Like

Typos happen. That’s one of my common typos

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