Iterate Through an Array with a For Loop: "total should equal 20"?

Iterate Through an Array with a For Loop: "total should equal 20"?
0
#1

Tell us what’s happening:

So I was able to complete this challenge no problem but, I just had a question about "total should equal 20", where do we get 20 from?

Your code so far


// 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];

// Only change code below this line


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop/

#2

If you sum all the numbers in myArr you will get 20, that’s where you get it from. When you use thefor loop to iterate thru this array you are just summing all the array values into a variable as you can see in the example.

1 Like
#3

Okay that makes sense, thank you. For the example above the challenge, the sum is 42 correct?

#4

Yes the sum of 9, 10, 11 and 12 is 42.

1 Like