Javascript 正在努力理解这个for循环是如何工作的
在CodeAcademy上遇到了这个for循环问题,无法计算出总数是如何达到23的。如果有人能帮我解决这个问题,我会很感激的Javascript 正在努力理解这个for循环是如何工作的,javascript,arrays,for-loop,Javascript,Arrays,For Loop,在CodeAcademy上遇到了这个for循环问题,无法计算出总数是如何达到23的。如果有人能帮我解决这个问题,我会很感激的 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);
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);
在回顾这件事时,你可能会错过一些东西。首先,一次递减2。其次,索引从0开始,因此6的索引实际上是数组的第7个元素。
/=
运算符将值除以,并将结果保存在变量中。+=
运算符将值相加并将结果保存在变量中。只要你理解了所有这些要点,你就应该能够自己解决这个问题,我建议你这样做。如果你需要帮助,你可以参考下面的内容,但我恳请你自己试试
最初,i=6
第一次迭代:
i is 6;
values[6] is 5;
total /= 5 is 20;
logs(20);
total += 10 is 30;
i is 4;
values[4] is 15;
totals /= 15 is 2;
logs(2);
total += 10 is 12;
i is 2;
values[2] is 4;
totals /= 4 is 3;
logs(3);
total += 10 is 13;
i is 0;
values[0] is 1;
totals /= 1 is 13;
logs(13);
total += 10 is 23;
第二次迭代:
i is 6;
values[6] is 5;
total /= 5 is 20;
logs(20);
total += 10 is 30;
i is 4;
values[4] is 15;
totals /= 15 is 2;
logs(2);
total += 10 is 12;
i is 2;
values[2] is 4;
totals /= 4 is 3;
logs(3);
total += 10 is 13;
i is 0;
values[0] is 1;
totals /= 1 is 13;
logs(13);
total += 10 is 23;
第三次迭代:
i is 6;
values[6] is 5;
total /= 5 is 20;
logs(20);
total += 10 is 30;
i is 4;
values[4] is 15;
totals /= 15 is 2;
logs(2);
total += 10 is 12;
i is 2;
values[2] is 4;
totals /= 4 is 3;
logs(3);
total += 10 is 13;
i is 0;
values[0] is 1;
totals /= 1 is 13;
logs(13);
total += 10 is 23;
最终迭代:
i is 6;
values[6] is 5;
total /= 5 is 20;
logs(20);
total += 10 is 30;
i is 4;
values[4] is 15;
totals /= 15 is 2;
logs(2);
total += 10 is 12;
i is 2;
values[2] is 4;
totals /= 4 is 3;
logs(3);
total += 10 is 13;
i is 0;
values[0] is 1;
totals /= 1 is 13;
logs(13);
total += 10 is 23;
循环后:
logs(23);
定义了第一个数组
值
和变量总计
// `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;
然后创建一个for循环
for (let i = values.length - 2; i >= 0; i -= 2)
它由三部分组成-
-当for循环开始时仅运行一次的初始步骤让i=values.length-2
- 它创建变量
i
- 然后将
值的长度分配给
数组减去2
- 它创建变量
-条件-每次迭代之间检查的内容,如果不正确,循环结束。这里它检查i>=0
是否小于或等于零i
-这是它在每次迭代后所做的操作-在这里,它将i-=2
的值降低2(使用i
运算符,其作用与-=
相同)i=i-2
a
被分配到值
数组中当前i
索引上的任何值
// if `i` would be 2 in this iteration then `a` would be assigned 4
// because it counts from 0
let a = values[i];
然后将总计分配给它的值除以a
,然后将其记录下来
// same as `total = total / a`
total /= a;
最后,total
被赋予其值+10的新值
total += 10;
让我们一起进行第一次迭代
// 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
现在您自己为每个迭代尝试此方法,直到循环结束
有关更多信息,请参阅这些资源
循环直到i>=0,在每个循环中将i减少2
因此,每个循环中的
a
值将相应地成为值的第七、第五、第三和第一个元素,因此:5、15、4、1
因此,在每个循环中进行的计算如下:
100 / 5 + 10 = 30
30 / 15 + 10 = 12
12 / 4 + 10 = 13
13 / 1 + 10 = 23
你为什么不自己走一遍呢?换句话说,拿一支铅笔和一张纸,把每一个步骤都写下来——没有那么多。也可以在调试器中逐步完成,但我鼓励实际写作。这是所有领域中最明智的评论^^谢谢你,我很感激。不用担心,但正如我所说的和@Dave Newton所说的,请在纸上完成。这将有助于你真正理解世界。不学习,理解更重要。一些空行和/或段落会让这更容易理解。