Stuck on iterate through an array with a for loop challenge [SOLVED]

I’m stuck on this challenge link here https://www.freecodecamp.com/challenges/iterate-through-an-array-with-a-for-loop#?solution= %2F%2F%20Example var%20ourArr%20%3D%20[%209%2C%2010%2C%2011%2C%2012]%3B var%20ourTotal%20%3D%200%3B for%20(var%20i%20%3D%200%3B%20i%20<%20ourArr.length%3B%20i%2B%2B)%20{ %20%20ourTotal%20%2B%3D%20ourArr[i]%3B } %2F%2F%20Setup var%20myArr%20%3D%20[%202%2C%203%2C%204%2C%205%2C%206]%3B %2F%2F%20Only%20change%20code%20below%20this%20line var%20total%20%3D%200%3B for%20(var%20x%20%3D%200%3B%20x%20<%20myArr.lenght%3B%20x%2B%2B)%20{ %20%20total%20%2B%3D%20myArr[x]%3B }

I’ve declared and initialized total to 0 and I used a for loop to iterate through myArr but my total keeps coming up as equal 0 and I don’t know how to get total to equal 20. I would be most grateful for any help. Here is the code I used

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 x = 0; x < myArr.lenght; x++) {
  total += myArr[x];
}

your code is fine except for the typo at

for (var x = 0; x < myArr.lenght; x++) {
total += myArr[x];
}
change lenght to length
and console.log(total) should give you 20.
good luck.

1 Like

Thank you so much @sisav that was it! I thought I was going crazy.