简体   繁体   English

此JavaScript代码的含义是什么?

[英]What is the meaning of this JavaScript code [on hold]

 var total = 0; var total2 = 0; for (var number = 2; number < 15; number++) { total += number; for (var number2 = 1; number2 <= 12; number2++) { total2 = total + number2; } } 

I'm trying to figure out the value of total,total2, number, and number2. 我试图找出total,total2,number和number2的值。 I don't expect an answer, I just want to know how to find the answers. 我不希望得到答案,我只想知道如何找到答案。

At the end of the script to display the result in the console. 在脚本末尾以在控制台中显示结果。

console.log(total, total2);

While this is not about what's the final result. 虽然这与最终结果无关。 The meaning is total is the sum of every numbers between 2 and 14 and total2 is the sum every between 1 and 12 for each loop (like a multiply). 含义是total是每个循环中2到14之间的每个数字的总和, total2是每个循环中1到12之间的每个数字的总和(如乘法)。

Of course, it has a purpose yes. 当然,它的目的是肯定的。 Who knows why someone write this. 谁知道为什么有人写这个。

 var total = 0; var total2 = 0; for (var number = 2; number < 15; number++) { total += number; for (var number2 = 1; number2 <= 12; number2++) { total2 = total + number2; } } console.log('total:',total) console.log('total2:',total2) 

This is an example of a nested loop. 这是嵌套循环的示例。

The outer for loop is run thirteen times for the values of number from 2 to 14 . for 214number值,外部for循环运行13次。

The inner loop is run twelve times per iteration of the outer loop. 内循环在外循环的每次迭代中运行十二次。

The final value of total will be the sum of the numbers 2 to 14 ( 104 ). total的最终值将是数字214104 )的总和。

total2 will be overwritten twelve times per iteration of the outer loop. 每次外部循环迭代时, total2将被覆盖十二次。

The final value of total2 will be 104 (the final value of total1 ) plus 12 (the final value of number2 ) = 116 . 的最终值total2将是104 (终值total1 )加上12 (的最终值number2 )= 116

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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