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: