Basic JavaScript - Iterate Through an Array with a For Loop

This challenge is dumb and the logic presented here Learn JavaScript - Full Course for Beginners - YouTube makes no sense. someone please explain how the var i can tell the what all of the myArray numbers added together equals.

Challenge: Basic JavaScript - Iterate Through an Array with a For Loop

Link to the challenge:

1 Like

If you where to add the elements the hard way, you could do:

const arr = [1,2,3,4]
let total = 0;
total =  total + arr[0]
total =  total + arr[1]
total =  total + arr[2]
total =  total + arr[3]

Now, let’s say we can tell the code: hey what if I tell you how and you add them?

Something like this:

const arr = [1,2,3,4]
let total = 0;
"do it for i=0 to i=3!"
total =  total + arr[i] 

When you loop, that is done for you to some extent. The line between "" needs to be replaced for the right instruction.

Don’t get desperate, it is difficult, but that is almost a definition of coding…

3 Likes

Okay, I was thinking of values in the array as numbers, so for example
const arr = [160, 52]
let total = 0;
total = total + arr[1]
console.log(total);

When looking at the problem mentioned above, I was imagining that the console would print 2, not 52. After seeing how you laid out the problem it makes a lot more sense now, Thank You : D

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.