JS: Help (explanation please):NEW [spoiler]

var myArray=[2, 3, 4, 5, 6]
var ourTotal=0

for (var i=0;i<myArray.length;i++){
  ourTotal+=myArray[i]
  }
console.log(ourTotal)

So, could somebody explain what this for statement is doing? I have attempted to understand it, but couldn’t get past the part which says ourTotal=myArray[i]. Could somebody please explain?

Hello!

Let me see if I can help…
for (var i=0; i<myArray.length;i++)
This sets up a loop. i starts at 0, and the loop will run as long as i is less than the length of myArray. At the end of each loop, i++ increases the value of i by 1.

ourTotal+=myArray[i]
This adds the value of myArray at index i to the ourTotal variable.

So, in plain terms, the loop counts through each value in the array and adds that value to the running total.

3 Likes

Howdy there! @nhcarrigan did a pretty good job explaining how the code works. Since you are new, here is a full detailed explanation:
This declares the variable myArray that has a value of an array(contains multiple values encased in brackets []) of 2,3,4,5,6.

var myArray=[2, 3, 4, 5, 6]

This declares another variable called ourTotal. This one is a placeholder variable and usually is going to be changed. I know this because at the bottom of the code, ourTotal is called and being modified.

var ourTotal=0

This is a for loop. Here’s how the sequence go.

  • It first declares a variable named i and sets it to 0.
  • Then it checks if the variable i(which has a value of 0) is smaller than the length of the variable myArray. Since myArray has 5 numbers in them, that means the length is 5.
  • Then after it runs, adds 1 to i (++ is just a shortcut of var = var + 1;)
  • Then it runs the code inside the loop.
  • Inside the loop, it is set to add myArray[i] i is the number to select which value to select from the array, which from the declaration i is 0. Javascript is a 0 index language, that means 0 is the first one instead of 1. So it adds myArray[0] which is 2, into theourTotal.
  • then it checks if i has the condition. It will do this until the condition is not right anymore, which is i is bigger than myArray.length
for (var i=0;i<myArray.length;i++){
ourTotal+=myArray[i]
}

Then at last this just logs the variable ourTotal to the console.

console.log(ourTotal)
2 Likes

Thanks, but how do I figure out the index of i (is it 0 because i=0)? Also, ourTotal is worth 0 though (said in second line of code), so shouldn’t the console output be ourTotal (0)+myArray index i (i=2)=2?

However, you really did do a great job of explaining the first part

The i variable is used to access the index of the array.
In the example you have, i initialises with a value of 0. myArr[i] would translate to myArr[0], which means “the value in position 0 of myArr”. In this case, that value is 2.
ourTotal gets initialised with a value of 0, and we add the myArr[i] to that value.
So ourTotal = myArray[0] is 0+2.
Then on the second loop, ourTotal = myArray[i] would be 2 + 3, making ourTotal 5.

1 Like

OHHHHHHHHHHH! So the for statement basically makes the commands which the loop can run through, then, once the curly braces are declared, you are looping the array, and increasing the index once each time. If this explanation doesn’t make sense, basically, the easiest way I could explain it is that i is myArray’s index is changing each time it loops though.

So it keeps on looping until the function isn’t fulfilled, right? It keeps on going until the for statement stops it.

Correct! The loop runs as long as the i < myArr.length value is true.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums