Iterate through an array with a for loop

I was able to solve this by understanding about 70% and looking at the hints for the rest. Id appreciate if someone could explain the code in laymen terms so i can understand it better.

var myArr = [ 2, 3, 4, 5, 6];
var total = 0;
for (var i = 0; i < myArr.length; i++){
  total += myArr[i]; 
}

I understand what is happening but i dont understand the part of that code that is taking the first number of the array and adding it to the total.

Not sure if this answers your question, but total += myArr[i] is just shorthand for total = total + myArr[i].

On the first pass through the for loop, the current total, which is 0, is added to the value that is in myArr[0], which is 2. The variable “total” is then updated with this new value. It continues to update for each item in the array.
Hope that helps…

2 Likes

So its cycling through the variables in myArray one at a time (2, 3, 4, 5, 6) ?

and myArr[0] = 2… because it is 0 indexed? i think im getting it…

and once it goes through 5 times…and since we added 2, 3, 4, 5, 6 to the total… it comes to 20. hot dang. i think i got it… thank you

1 Like

@NerdBurglur Are you familiar with parts of the for loop?

for (Initialization; condition; increment) 
{
     loop body; // code here will be executed if the condition is true
}

Initialization = A variable is declared here. This variable will be used in the the condition part of the loop and should also be in the increment as well as the body of the loop. The letter i is normally used but you can use any valid variable name.

(var i = 0;

Condition = The condition in the for loop is checked. If the condition is true, the body of the loop is executed. If it’s false the code skips the for loop and continues execution of the program after the closing curly brace ‘}’ of the for loop. It basically acts as if the for loop was not present and keeps executing the rest of the program.

; i < myArr.length ;

In this case Javascript checks if the value of i is less than the length of the array (myArr.length.)

Increment = This part will change the variable each time the body of the loop is executed. It can increase or decrease the variable. In this case var i is increased by 1 each time the loop runs.

; i++)

You have to make sure the logic will eventually become false otherwise you end up with and endless loop.

var myArr = [ 2, 3, 4, 5, 6];  
var total = 0;                 
for (var i = 0; i < myArr.length; i++){
total += myArr[i]; 
}
  1. Variable i, which currently has a value of 0, is compared to the value of myArr.legth. The value of myArr.length is 5 because there are 5 elements in the array. The condition evaluates to true because 0 is less than 5, so the body of the for loop is executed.

  2. The body of the loop takes an element of the array and adds it to total. myArr[i] is the same as myArr[0] because i = 0 The value of myArr[0] is 2. So total which was set to 0 is now equal to 2.

  3. The loop reaches the end so the increment i++ adds 1 to i. i is now equal to 1.

  4. The program then goes to the top of the loop and checks the condition again. Since i is now equal to 1 and 1 less than 5 (myArr.length) the body of the loop is executed again.

  5. This time around total is equal to 2 and myArr[i] has a value of 3 because myArr[i] = myArr[1] = 3

  6. The loop reaches the end again and i is incremented by 1 again. i is now equal to 2.

It’s a little long. Hope it helps.

14 Likes

Totally helps. Thank you very much.

i was specifically wondering what tells it to go through the array number by number. How does it know to move on in the array and not keep solving for for 0 (2)

is it just because its the next step and thats how the language was written?

The three parts of the for loop work together to do this but really the increment i++ controls this part.

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

Here I changed the increment so 2 is added to i on each iteration of the loop.
total will equal 2 after the first loop.
On the next iteration, myArr[i] is equal to myArr[2]. myArr[2] has a value of 4. since total has a value of 2, 2 + 4 = 6.

At the end of the second iteration 2 is add to i, which was 2 so i = 4.

You can make the increment anything you like in order to control how the loop behaves.

You can make it i--, or i = i * 2 it depends on what you want the loop for.

In this case you want to go through each element in the array so you simply add 1 to i each time the loop runs.

This loop would print every number from 1 to 19.
for ( x = 1; i < 20; x ++) {
console.log(x);
}

If you change the increment to increase by 2 you only print even numbers.

for ( x = 1; i < 20; x = x + 2) {
console.log(x);
}
2 Likes

I was just trying to grasp what part of the language tells it to move along in the array itself (2, 3, 4, 5, 6) … from 2 onto 3 to 4 to 5 etc… im sorry im not getting it

i just get really stuck on minutia of “how” and “why”

side note. Is ( i < myArr.length) only there to provide means to an end for the problem?

@NerdBurglur Yes. Once the condition evaluates to false the loop stops running.

If the the condition is false from the beginning the loops never runs. for loops are generally used when we want to execute something (code inside body of loop) a set number of times since we control the initial value of the iterator (var i) and we can also control the condition as well as the increment after each loop.

1 Like

perfectly explained thank you.

1 Like

Hey! I’m having a hard time understanding this as well. Every time I run through it, it comes up with a different total. Can you explain how the total is being calculated on each loop?

Can you post your code?

My code is correct. My trouble is when I actually try to do the calculations myself using the code. I don’t understand how the code is coming up with the answer ‘20’. I get either ‘18’ or ‘25’. I think I tried to go through it based on your explanation above and I got an answer other than ‘20’.

very helpful. Thanks.