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: