So I was able to complete the challenge with this code:
// Example
var ourArr = [ 9, 10, 11, 12];
var ourTotal = 0;
for (var i = 0; i < ourArr.length; i++) {
ourTotal += ourArr[i];
}
// Setup
var myArr = [ 2, 3, 4, 5, 6];
var total = 0;
for (var i = 0; i < myArr.length; i++) {
total += myArr[i];
}
Now my question is, how does this formula know to stop and get ‘total = 20’? Does the formula cap out at the sum of all of the integers in the array?
I may have misunderstood everything but I think that in this example, myArr.length is 5 and myArr has an index of 4.
// Setup
var myArr = [ 2, 3, 4, 5, 6];
Most likely a slip up… in the //Example myArr.length does have a length of 4, while in //Setup it’s a length of 5.
So yeah, for //Setup, i < myArr.length stops being true and breaks out of the loop once i reaches a value of 5, meaning it only does 4 iterations and the total is 20.