Iterate Through an Array with a For Loop - Didn't declare total?

Tell us what’s happening:

Not sure what’s wrong. It says my result correctly totals to 20. But it fails me for " total should be declared and initialized to 0". It seems to me like I declared total and set it to 0. What’s the problem?

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

var total = 0
for (var i = 0; i < myArr.length; i++) {
  total += myArr[i];
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0.

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

You are missing a semicolon (;).

1 Like

Thank you! I thought those were generally optional; guess this is a good reminder to use them anyway.

As a good practice, always use the semicolon. They might seem optional, but they are not and this simple gesture can avoid a lot of headaches.

By the way, welcome to the FCC forum :slight_smile:

Hi there,
Also a good practice will be to start also at the same time working with modern JS, although i will said i love the regular for loop. I will still think that for best practices to work on other options to create a for loop. Just to understand, many companies might sometimes ask developers for modern JS, and for good practices its good to have alternatives and options, in this case the for loop vs forEach loop.

This will help developers to just dont get stuck in just one option, or maybe when you are reading in someones code, and they have something new for ES6,ES7 will be good to know this new coding upgrades. ? what all think about that. Let me know.

the curriculum introduces array methods later, first it’s good to know how loop works.