Iterate Through an Array with a For Loop - BUG

Iterate Through an Array with a For Loop - BUG
0.0 0

#1

Tell us what’s happening:

The test is failing if total is declared with “let” instead of “var”.

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(let i=0; i < myArr.length; i++){
  total += myArr[i];
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0.

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


#2

Your code passes when I run it. Try clearing the challenge and putting your solution above in again.


#3

Yes, it runs with the code that was submitted. I had to change it to pass, but wanted to let you all know about the bug with variable declaration using ‘let’.


#4

I thought you were saying that it doesn’t pass when you use let i = 0 in your loop. It does.
It also passes if you change it to var i = 0.


#5

Yes, when I use “let i = 0” in the loop it fails to pass, when I use “var i = 0” in the loop it passes. I wanted you to know about the issue where “let” is not recognized as a valid declaration.


#6

As I said in my first response, the tests pass with your code including using let. I don’t know what else might have been going on to make your tests fail the first time, but the tests allow you to use let.