Iterate Through an Array with For Loop

Tell us what’s happening:

Help me on this, i cant get the answer correct

Your code so far

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

var total;

// Only change code below this line

for (var i = 0; i < myArr.length; i++) {

total += myArr[i];

}

// 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;
// Only change code below this line

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


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36</code>.

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

what are you trying to get here.
Tell me your expected output

I want this

Declare and initialize a variable total to 0 . Use a for loop to add the value of each element of the myArr array to total .

Hey tau, you should declare total = 0 and then execute, that should work

Thanks so much it worked

1 Like