Why Won't My Code Iterate Through an Array with a For Loop?

Why Won't My Code Iterate Through an Array with a For Loop?
0

#1

I ran this code for the section “Iterate Through an Array with a For Loop”, but it keeps saying that total should be declared and initialized to 0, and not to set it to 20. I have done precisely both of those things, but it’s still giving me an error.

// Setup
var myArr = [ 2, 3, 4, 5, 6];

// Only change code below this line
var total = 0

for (var i = 0; i < myArr.length; i++) {
total += myArr[i];
}


#2

In the future, also post the challenge url or use the Ask for Help button on the challenge? That way, we don’t have to search for the challenge and your code would automatically populate in the post.

Thank you.


#3

missing a ; on var total = 0

The tests can be picky


#4

No problem, thank you for the tip; I’m new here. I will adhere to it from now on.

Cheers,

ALex


#5

I still don’t quite understand what that means; do I replace the 0 with an a? It returns undefined when I do that. I put in myArr, but it still doesn’t return all green. How would you write the line that’s messing the program up?


#6

You are still missing a ; .


#7

Oh my god, I thought you meant I was missing “a”, as in myArr or something like that. Sorry about that, and it runs with no problems.

Thank you very much for that! I was breaking my head for almost an hour on this


#8

Don’t worry man. Happens to the best of us.

Although, I thought the FCC challenges highlight simple syntax errors for you. That’s what the red squiggly lines are.