I don't understand: Iterate Through an Array with a For Loop

Tell us what’s happening:

//logic here
2 3 4 5 6
0 1 2 3 4
The 0 is the first number because of zero indexing it’s should be equal to 2
Yet when i try this it won’t work why not? the counting starts at 0
-1 the same result

Your code so far


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

Your browser information:

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

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

you are not printing to the console the total variable, but you are printing myArr[total] and that is the element in the array

you did a weird choice with using the name total for the variable to be used as array index

1 Like