Iterate Through an Array with a For Loop - BUG

Iterate Through an Array with a For Loop - BUG
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

0 Likes

#2

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

0 Likes

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

0 Likes

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

0 Likes

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

0 Likes

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

0 Likes

#7

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

}

0 Likes

#8

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.

0 Likes

#9

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

0 Likes

#10

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