For Loops clarification

Tell us what’s happening:
Describe your issue in detail here.

The instructions say to declare and initialize a variable total to 0. But doesn’t “initialize” mean to put it inside the for loop parenthesis as the initialization?

  **Your code so far**

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

// Only change code below this line

let total = 0;
for (let i = 0; i < myArr.length; i++) {
total += myArr[i];
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Iterate Through an Array with a For Loop

Link to the challenge:

“initialize” just means to give it a starting value when you declare it. It doesn’t have to ne in a for loop. In the code you have, myArr, total, and i are all initialized, just in different places.

1 Like

To initialize it just means to give it an initial value. It needs to be initialized as it would be undefined otherwise and you can’t work with it. If you initialize it inside the for loop it would be initialized each time the loop runs.

If you’re talking about these paranthesis
(let i = 0; i < myArr.length; i++)
then no. Nothing else gets initialized there other than the i which is your number of iterations that the for loop does.

1 Like

Thanks for both answers. Makes sense now.

1 Like

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