This is so weird

Tell us what’s happening:
var myArr = [ 2, 3, 4, 5, 6];
//why this retuns 0 ?
/*var total = 0;

for (var i = 0; i < myArr.lenght; i++) {
total += myArr[i];
}
console.log(total);*/

// and this returns 20?

var total = 0;

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

console.log(total);

Your code so far


// Setup
var 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/88.0.4324.104 Safari/537.36.

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

1 Like

Hi, it’s not weird as your code spells wrong which the keyword should be length not lenght.

1 Like

I must be tired. thanks mate

1 Like

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