// Setup
const myArr = [2, 3, 4, 5, 6];
// Only change code below this line
var total = 0
for (var i = 0; i < myArr.length; i++) {
total += myArr[i];
}
However, I am just confused as to how the code "total += myArr[i] can equate to 20 in the consolelog.
Can anyone kindly explain this?
console.log(total)
total starts out as 0
total = 0
Then the for loop runs from i = 0 to i = length of the array less 1
within the for loop the total is getting incremented each time by the value of the current number myArr[i]
So a table showing what happens may show
value of total
0
0 + 2
2 + 3
5 + 4
9 + 5
14 + 6
20