简体   繁体   中英

struggling to understand how this for loop works

Ran into this for loop question on code academy and can't figure out how the total comes out to 23. If anyone could walk me through this I'd appreciate it.

const values = [1, 10, 4, 3, 15, 3, 5, 2];

let total = 100;

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

console.log(total); 

There are a few things you could have missed when going over this. Firstly, you are decrementing by 2 at a time. Secondly, indexing starts at 0 so the index of 6 is actually the 7th element of the array. The /= operator divides the value and saves the result in the variable. The += operator adds to the value and saves the result in the variable. So long as you understand all of these points, you should be able to walk this through yourself and I recommend you do. If you need some help, you can refer to the below, but I implore you to try it yourself.

Initially, i = 6 .

First iteration:

i is 6;
values[6] is 5;
total /= 5 is 20;
logs(20);
total += 10 is 30;

Second iteration:

i is 4;
values[4] is 15;
totals /= 15 is 2;
logs(2);
total += 10 is 12;

Third iteration:

i is 2;
values[2] is 4;
totals /= 4 is 3;
logs(3);
total += 10 is 13;

Final iteration:

i is 0;
values[0] is 1;
totals /= 1 is 13;
logs(13);
total += 10 is 23;

After loop:

logs(23);

First array values and variable total are defined

// `const` because it won't change after the initial assignment
const values = [1, 10, 4, 3, 15, 3, 5, 2];

// `let` because it will be changed
let total = 100;

Then a for loop is created

for (let i = values.length - 2; i >= 0; i -= 2)

It is made of 3 parts -

  • let i = values.length - 2 - the initial step that is ran only once when the for loop begins
    • it creates the variable i
    • then assigns the length of the values array minus 2
  • i >= 0 - the condition - what is checked between every iteration, if it's falsy the loop ends. Here it checks if i is smaller than or equal to zero.
  • i -= 2 - this is what the it does after each iteration - here it descends the value of i by two (using the -= operator which does the same as i = i - 2 )

Upon every iteration the local variable a is assigned whatever value is on index of the current i inside the values array.

// if `i` would be 2 in this iteration then `a` would be assigned 4
// because it counts from 0

let a = values[i];

Then total is assigned it's value divided by a , then it's logged

// same as `total = total / a`
total /= a;

and finally total is assigned a new value of it's value + 10.

total += 10;

Let's go through the first iteration together

// length of `values` is 8 (here it counts from 1)
let i = 8 - 2  // 6

// what's at position 6 in `values`? the number 5 (counting from 0 again)
let a = 5;

// `total` is still 100 (hasn't been changed yet)
total = 100 / 5  // 20

// `total` is now 20
total = 20 + 10  // 30

Now try this yourself for each other iteration until the loop ends


Refer to these resources for more information

You start looping with i=6 cause values.length is 8
You loop till i>=0 decreasing i by 2 in each loop
so the a value in each loop will be accordingly the 7th, 5th, 3ed and first element of values, so: 5, 15, 4, 1 thus, the math done in each loop will be as follows:

100 / 5 + 10 = 30
30 / 15 + 10 = 12
12 / 4 + 10 = 13
13 / 1 + 10 = 23

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM