# Exercise of javascript: Iterate Through an Array with a For Loop (what's wrong?)

**URL:** <https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855>\
**Category:** General\
**Created:** [July 29, 2017, 3:34am UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855 "2017-07-29T03:34:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Verie707](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/verie707/32/59770_2.png) [@Verie707](https://forum.freecodecamp.org/u/Verie707)\
**Post date:** [July 29, 2017, 3:34am UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855/1 "2017-07-29T03:34:27Z")

</div>

Instructions  
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.

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

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [July 29, 2017, 3:48am UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855/2 "2017-07-29T03:48:31Z")

</div>

```auto
for (var x=0; x < myArr.lenght; x++){

```

You may want to check the speling on that line. 😉

Common mistake, we all do it at least once. I do it at least once a day.

---

<div class="post-metadata">

**Author:** ![Verie707](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/verie707/32/59770_2.png) [@Verie707](https://forum.freecodecamp.org/u/Verie707)\
**Post date:** [July 29, 2017, 4:18am UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855/3 "2017-07-29T04:18:12Z")

</div>

Oh my god Thank you. I was stuck because of that \*facepalm 😂

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [July 29, 2017, 4:25am UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855/4 "2017-07-29T04:25:49Z")

</div>

It happens. Trust me, it won’t be the last time. One frustrating thing about JS is that it lets things like this slide and you have to hunt for them.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:47pm UTC](https://forum.freecodecamp.org/t/exercise-of-javascript-iterate-through-an-array-with-a-for-loop-whats-wrong/138855/5 "2021-01-17T20:47:03Z")

</div>

Closed on January 17, 2021, 8:47pm UTC


