Iterate Through an Array with a For Loop - BUG

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

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

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

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.

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.

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.

// 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];

}

In the future, please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Ask for Help button located on each challenge. This will automatically import your code in a readable format and pull in the challenge url while still allowing you to ask any question about the challenge or your code.

Thank you.

Hi,

I’m afraid there is still a bug. I came here just because I wanted to let you know.

The test couldn’t be passed with “let” so I had to declare “var”.

You cannot declare and initialize the variable total with let because the test is looking for a specific line. You can use let in the loop without any errors.

1 Like