var myArr = [2, 3, 4, 5, 6];
for (var i = 0; i < myArr.length; i++)
So since the code uses base 0 counting, i=0
tells the code to start with the first place in the array, which in this example is [2]
. i=1
would be place number two or [3]
. i=2
would be place number three or [4]
. Rinse and repeat until you’ve reached i=4
which is the last place. Since myArr.length
counted five places in the array, and you’re at i=4
(place number 5), the middle part of the for loop i < myLength;
tells the code to stop before it gets to i=5
(or place number 6).
To find the our answer output or total=20
you need to look at the portion that comes after the for loop setup: total += myArr[i];
.
This tells the code that in each loop, you’ll need to add which ever spot in the array you’re at to the already established total
. So with loop number one, you have 0
(remember, total
was set equal to 0
at the beginning) being added to 2
because you’re using the item in the first spot in the array. This gives you total = 2
. You have now stored the number two in the total
variable.
After the code has calculated the total
for this loop, it refers back to the i++
and then adds 1
to i
. (Remember, you’re using Base 0 counting, so the [2]
is actually at place number 0, etc.) If we’re starting at the beginning, this adds 1
to i=0
giving you i=1
and the begins the next loop with for (var i=1; i <= myArr.length; i++)
. (This is only used when working through the loop so that the code knows where it’s at in the array and when to stop running.)
Now it repeats the same process for each part of the array, adding along the way.
Loop 0: Find the item in the first place: 2.
Count the number of places in the array: 5.
Add the item from the first place to the stored total=2
: 0 + 2.
Return to for loop
instructions and complete i++
. i=0
and i++
means to add 1
to i
, so you now have i=1
. On to the next loop.
Loop 1: Find the item in the second place: 3. (because we now have i=1
from the previous loop)
Count the number of places in the array: 5.
Add the item from the second place to the stored total=2
from loop 0: 2 + 3 = 5.
Return to for loop
instructions and complete i++
. i=1
and i++
means to add 1
to i
, so you now have i=2
. On to the next loop.
Loop 2: Find the item in the third place: 4. (because we now have i=2
from the previous loop)
Count the number of places in the array: 5.
Add the item from the third place to the stored total=5
from loop 1: 5 +4 = 9.
Return to for loop
instructions and complete i++
. i=1
and i++
means to add 1
to i
, so you now have i=3
. On to the next loop.
Loop 3: Find the item in the fourth place: 5. (because we now have i=3
from the previous loop)
Count the number of places in the array: 5.
Add the item from the fourth place to the stored total=9
from loop 2: 5 + 9.
Return to for loop
instructions and complete i++
. i=1
and i++
means to add 1
to i
, so you now have i=4
. On to the next loop.
Loop 4: Find the item in the fifth place: 6. (because we now have i=4
from the previous loop)
Count the number of places in the array: 5.
Add the item from the fifth place to the stored total=14
from loop 3: 6 + 14 = 20.
Return to for loop
instructions and complete i++
. i=1
and i++
means to add 1
to i
, so you now have i=5
. Now because i<myArr.length
tells the code to only run when i
is less than the length of the array and no larger, it stops. If we were to continue, i
would equal five and that would make the statement no longer true
. So it’s gotta stop.
Finally, we return total = 20
.
1: I REALLY hope this is all correct. It’s only my understanding of it all plus some research, but Math and I don’t always get along.
2: I’m sorry that it’s so long, but I wanted to make it was thorough and accessible.
If you still have a hard time understanding it, lemme know and I’ll see if I can do it some other way!!