Basic JavaScript - Iterate Through an Array with a For Loop

I solved this problem by imitating the logic, but am seriously struggling to wrap my head around “loops” and some of its concepts. Please help.
I understand that “loops” have a structure, but if you are adding all the elements in an array why are you supposed to use (<) in " i < myArr.length" instead of (=) or (<=). How can you add all of the elements if the logic is stopping one element before the last??? Also, what is the “i++” doing here?
Your code so far

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

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

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

console.log(total);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15

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

Link to the challenge:

What is the index number of the first element in an array? Based on that, what would be the index number of the last element in the array?

Increment a Number with JavaScript

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