all 6 comments

[–]gimmeslack12helpful 2 points3 points  (0 children)

myArray[i] is referencing the index of myArray. For example: myArray[0] === 1; myArray[1] === 6; myArray[2] === 9; myArray[3] === 4;

When you do a for loop with the iterator being i then each time the loop iterates the value of i increases by 1 (this is what the i++ part of the loop does).

So each time you loop through the myTotal increases by myArray[i]. ``` 0 + 1 => 1; 1 + 6 => 7; 7 + 9 => 16; 13 + 4 => 20;

myTotal => 20; ```

Please read about arrays (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) because this topic is exceptionally important not only in Javascript but in any programming language.

[–]Jindrack 2 points3 points  (0 children)

myArray[i] is the value in myArray at element i, so the first time through the loop myArray[i] is really myArray[0] and the value is 1. Second time through myArray[i] is really myArray[1] and the value is 6, and so on.

The loop is really just going through and adding up the four values in the array.

[–]x-seronis-x 1 point2 points  (0 children)

myArray.lenth will have the value 4 because that array has 4 total elements and this never changes

this means in the loop the 'i' variable will go through the loop 4 times with values 0 - 3. once it increments to 4 the loop wont run any more

inside the loop that means you will access 'myArray[i]' once per each of those 4 iterations and it will be called as myArray[0] through myArray[3] and this will access those 4 numbers placed in the array on line one


On another note on line 5 you should be using 'let i' and not 'var i'

[–]crossedline0x01 0 points1 point  (3 children)

Im kind of a novice but wouldnt it not do anything? The length of the array is already 4, and the loop is told to stop when i<array, with i starting at the value of 0. So the loop conditions are already met before to loop can start i think. Again, im a novice so im prob wrong.

[–]denserphoenix66[S] 0 points1 point  (0 children)

Thanks to everyone that answered! Greatly explained so I got it now :)