Iterate through an array with for loop - where does it say to print total = x?

I understand how this function works, but what I don’t understand is where in the code does it say to print total = x to the console. I know that from this function total = 20. Shouldn’t total be called or something, though? Or at the very least just print “20” instead of “total = 20” because no where in the code does it actually say to print “total = x” where x is the number that the function results to when it’s done iterating.

var myArr = [ 2, 3, 4, 5, 6];
var total = 0;

for (var i = 0; i < myArr.length; i++) {
  total += myArr[i]; // does this tell the computer to print to console? 

Can you post a screenshot where you run this code and it displays “total = 20” to the console?

This is not the complete code, because there is no closing } for the for loop.

1 Like

I understand what you are referring to now. I looked the challenge. You are correct, that the code you posted above would not display “total = 20”. The FCC test is actually putting the "total = " part in the the “mock” console behind-the-scenes. It is just for demonstration purposes, but I can see how that could be confusing.