JavaScript for loops

I swear for loops are going to be the death of me. :upside_down_face:

This is a practice problem on the app, from the “Learn JavaScript” pack.

I get that the for loop will return 6, 4, 2, 0. I don’t understand values[i]. I understand what assigning it to a variable is doing, but not what it is doing and why we need it. I’m console logging through the steps and can’t understand why in the world console.log(a) at that point returns 5, 15, 4, 1.

let total = 100
for (let i = values.length - 2; i >=0; i -= 2) {
   let a = values[i];
    total /= a;
    total += 10;


You should also have a line like;
let values = [1, 3, 5, 7, 9, 11, 13, 15] ← (will have different values in here.)

So, in setting up the loop, we have;
let i = values.length - 2;
This means the iterator we’re using, i, is going to us the length of the values array, minus 2 as a starting point.
So, in the case of the example above, values has 8 items in it - so that’s its length.
We take 2 off that, so i begins the loop with a value of 6.

i >= 0;
Means we’ll keep looping whilst i is greater than or equal to 0. This is checked at the end of each loop, and we’ll only start a new one if it’s true, otherwise we’ll go on to the next piece of code.

i -= 2
How much we’re changing the iterator with each loop. So, in this case we’re actually subtracting 2 from it each time.

So, for i above, it would start at 6, as described above. Then each time it looped it would minus 2 until it was no longer greater than or equal to 0;
6 then 4 then 2 then 0, then it would stop as i = -2 no longer satisfies the requirement.

Now, inside your loop you have
let a = values[i];
It’s giving a the value that is at index i of the array values.
So, for the example above;
if i is 6, then values[i] would be 13.
Then as we loop through,
i = 4, values[i] = 9,
i = 2, values[i] = 5,
i = 0, values[i] = 1

It’s then using this value for a when making the calculations with total
total = total / a
total = total + 10

Hope this helps

Oh, ok! Thanks! That is a very thorough description, thank you so much for taking the time to do this.

That clears up values[i].

Here is the values line I forgot, sorry:
const values = [1, 10, 4, 3, 15, 3, 5, 2];

So a = values[i] is 5, 15, 4, 2 because those are the 6, 4, 2, and 0 indexes of the array. Got it.

When I get to total = total / a, how does total equal 20, 2, 3, 13? 100/5 = 20 obviously, but what is it doing after that to get to those other numbers? What am I not seeing?

That’s quite alright. Glad it helped. I’m just trying to avoid doing proper work, lol.
total is being changed within the loops.
So, it starts at 100, but it’ll be a different value each time it loops through.
So, first loop:
a = 5
total = 100 / 5
total = total + 10
so, total ends the first loop with a value of 30.

Next loop;
a = 15
total = total / a
is now;
total = 30 / 15
total = 2
total = total + 10
so, total is now 12

And next time;
a = 4
total = total / a
total = 12 / 4

lol :sweat_smile:

OK, so basically since it’s a loop, and defining a as values[i], it’s going to iterate all those values through the logic of the program until the loop is done, and that’s why console logging the one line looks weird and doesn’t make sense.

I guess I’m not connecting the dots properly when i is used outside of the loop. Thanks again for the clarification!