Basic JavaScript - Iterate Through an Array with a For Loop

ok if the total is equal to 0 than the condition would be if 0 is less then the length of my array increase it by 1 am I understanding this right? how do I get the total to be 20

Your code so far

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

// Only change code below this line
for ( let total = 0; total <myArr.length; total++){
console.log(myArr[total]);
}

Your browser information:

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

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

Link to the challenge:

“Declare and initialize a variable total to 0

You want to do this outside of the for loop.

“Use a for loop to add the value of each element of the myArr array to total.”

Inside the for loop you need to add the value of the number stored in the current array index to the running total variable. The example just prints that number out to the console. You want to add it to total.

for ( let total = 0;

You want to use a different variable name for the counter here. Most people would use i (just like in the example in the instructions).

ok so I declared the total to be 0 on the outside of my for loop.

so if i is equal to 0; the condition of 0< the length of the array increment it by 1? for some reason I keep logging 2

// 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++){
console.log(myArr[total]);
}

Describe in plain English (no code) what you are trying to log here.

I’m hoping that as you try to describe it you will realize where your mistake is.

I’m sorry im just not that smart and don’t understand. I want to log total to be equal to 20 do I need to redo a pass lesson to figure this out changed it to i but now im only logging 6

// 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++){
console.log(myArr[i]);
}

I know you aren’t going to pass this challenge yet. I’m trying to get you there incrementally, not all in one big step. I want to make sure you understand the basics first. Let’s don’t concentrate on the final answer just yet.

I see you made the following change:

console.log(myArr[i]);

Do you understand why you did that?

thanks so much for being patient with me. I changed that because I wanted to see what the console would out put for i.

OK, I’m going to assume you understand what that change did. So now you know how to access each number in the array as you loop through it. The final step is this:

“…add the value of each element of the myArr array to total.”

Do you know how to add a number to a variable that already has a number value?

For example, if I gave you the following code:

let myValue = 0;

What would you do to add 5 to myValue?

let myValue = 0;
I belive I would do
MyValue + 5;

This only adds 5 to the value that is in myValue, but it doesn’t change the value of myValue itself.

let myValue = 0;
myValue + 5;
console.log(myValue); // will print 0 instead of 5

ah ok then I would just change

let myValue = 0; myValue = 5;

OK, that is one way to do it. But now we want to add another number to myValue. And we don’t know what that number is because it is stored in the variable randomNumber which can be any random number.

let myValue = 5;
const randomNumber = Math.floor(Math.random() * 100) + 1;

Don’t worry about the functions on the right side of the equals sign if you don’t understand what is going on. The only thing you need to know is that they will create a random number between 1 and 100.

How would you increase the value of myValue by the number stored in randomNumber?

I think I would do

const randomNumber = Math.floor(Math.random() * 100) + 1;

myvalue = myValue + randomNumber

Perfect. That’s exactly what I was trying to get you to.

So let’s look at the instructions again:

“Use a for loop to add the value of each element of the myArr array to total.”

You’ve created the for loop and you know how to access each number in the array. Now you just need to “add the value of each element of the myArr array to total.”

Do you see how what I just had you do with myValue and randomNumber is basically the same thing that these instructions are asking you to do?

1 Like

Amazing! thanks so for much for you help you opened my eyes.

1 Like

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

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