.js for循环返回每个值中的两个

时间:2021-06-06 21:02:33

I am trying to write a for loop that returns values that double each time (1,2,4,8,16...) this code works but returns each value twice (2,2,4,4,8,8,16,16...) any idea what I am missing?

我正在尝试编写一个for循环,它返回每次加倍的值(1,2,4,8,16 ...)此代码可以工作但返回每个值两次(2,2,4,4,8,8, 16,16 ......)知道我错过了什么吗?

dub = function(howMany){
for(i = 1, y = 1, z = ''; z < howMany ; i = y, y = x, z ++ ){
    x = i * 2;
    console.log(x);
}};dub(20);

4 个解决方案

#1


I think you can use the Math.pow() function

我想你可以使用Math.pow()函数

dub = function (howMany) {
    for (var i = 0; i < howMany; i++) {
        console.log(Math.pow(2, i))
    }
};
dub(20);

Demo: Fiddle


If you don't want to use Math.pow(), you can use 2 variables, 1 as counter and another as the multiplier like

如果你不想使用Math.pow(),你可以使用2个变量,1作为计数器,另一个作为乘数

dub = function (howMany) {
    for (var i = 0, x = 1; i < howMany; i++, x *= 2) {
        console.log(x);
    }
};
dub(20);

Demo: Fiddle


Also note as suggested by @naomik below, you need to declare all the variables used in the loop as local scoped, else in can modify a variable with the same in another scope(like global or closure scope)

还要注意下面的@naomik所建议的,你需要将循环中使用的所有变量声明为本地作用域,否则可以在另一个作用域中修改相同的变量(如全局或闭包范围)

#2


Seems like you've overcomplicated the for loop:

好像你的for循环过于复杂:

function dub(howMany) {
    for(var i = 1, y = 1;i<=howMany;i++, y+=y) {
        console.log(y);
    }
}

I'm not clear on the increment/decrement portion of the for loop or why you assigned an empty string to z in the initialization. The reason your loop was returning duplicates is found by walking through the loop with the variable states in mind.

我不清楚for循环的递增/递减部分或为什么在初始化中为z分配了一个空字符串。你的循环返回重复项的原因可以通过循环遍历变量状态来找到。

Iteration 01 - i:1 x:0 y:1 z: <empty string>
Iteration 02 - i:1 x:2 y:2 z: 1
Iteration 03 - i:2 x:2 y:2 z: 2
Iteration 04 - i:2 x:4 y:4 z: 3
Iteration 05 - i:4 x:4 y:4 z: 4
Iteration 06 - i:4 x:8 y:8 z: 5
Iteration 07 - i:8 x:8 y:8 z: 6
Iteration 08 - i:8 x:16 y:16 z: 7
Iteration 09 - i:16 x:16 y:16 z: 8
Iteration 10 - i:16 x:32 y:32 z: 9

Note that the way your back-assigning y to i then x to y and so forth is unnecessary. Hopefully I've answered your question as perhaps you had other requirements not shared that required chaining those values like that, but not just for creating a doubling loop as described.

请注意,您将y分配给i然后x到y等的方式是不必要的。希望我已经回答了你的问题,因为你可能还有其他要求没有共享,需要链接这些值,但不仅仅是为了创建一个如上所述的双循环。

Here's a working JSFiddle: https://jsfiddle.net/f39jd848/

这是一个有效的JSFiddle:https://jsfiddle.net/f39jd848/

#3


Never mind all these answers showing how to write the code more simply. To answer your question, here's the order of operations that gives you two console logs of the same thing.

没关系所有这些答案显示如何更简单地编写代码。要回答你的问题,这里的操作顺序为你提供了两个相同的控制台日志。

For reference:

dub = function(howMany){
for(i = 1, y = 1, z = ''; z < howMany ; i = y, y = x, z ++ ){
    x = i * 2;
    console.log(x);
}};dub(20);

tl;dr: because the value of i is updated in the final-expression to be y BEFORE y is updated to be x (the result of the expressions in the loop). 2 iterations of the loop must pass before i equals a new value of x.

tl; dr:因为i的值在final-expression中被更新为y之前y被更新为x(循环中表达式的结果)。在i等于x的新值之前,循环的2次迭代必须通过。

Full walkthrough (hopefully no errors, I'm tired)

完整的演练(希望没有错误,我很累)

  1. During the first execution of this loop, none of the final-expressions (after the second ;) are evaluated. i equals 1, so x is assigned the value 2 (1*2).
  2. 在第一次执行此循环期间,不会计算最终表达式(在第二个;之后)。 i等于1,因此x被赋值为2(1 * 2)。

  3. The final-expressions are evaluated. i is assigned the value y which is 1 (the same value that i had on the first loop). y is assigned the value x, which is 2.
  4. 评估最终表达式。 i被赋值为y,即1(与第一个循环相同的值)。 y被赋值为x,即2。

  5. The contents of the for loop are evaluated again. Since i has been assigned 1 again, the result is the same, and 2 is logged again (and x equals 2 again.
  6. 再次评估for循环的内容。由于我再次被分配1,结果是相同的,并且再次记录2(并且x再次等于2)。

  7. The final-expressions are evaluated. i is assigned the value of y (now 2 from step 2), and y is assigned the value of x ( still 2 from step 2 ).
  8. 评估最终表达式。 i被赋值为y(现在是步骤2中的2),y被赋值为x(步骤2中仍为2)。

  9. the loop is executed again... i now contains the value 2 so x is assigned the value 4 and 4 is logged to the console.
  10. 循环再次执行...我现在包含值2,因此x被赋值为4,并且4被记录到控制台。

  11. final-expressions: i is assigned the value of y (still 2), then y is assigned the value of x (now 4).
  12. final-expressions:i被赋值为y(仍为2),然后y被赋值为x(现在为4)。

  13. This pattern continues...
  14. 这种模式继续......

#4


If you put this at the top of your for loop, it'll skip the second iteration:

如果你将它放在for循环的顶部,它将跳过第二次迭代:

if (z%2==1) continue;

#1


I think you can use the Math.pow() function

我想你可以使用Math.pow()函数

dub = function (howMany) {
    for (var i = 0; i < howMany; i++) {
        console.log(Math.pow(2, i))
    }
};
dub(20);

Demo: Fiddle


If you don't want to use Math.pow(), you can use 2 variables, 1 as counter and another as the multiplier like

如果你不想使用Math.pow(),你可以使用2个变量,1作为计数器,另一个作为乘数

dub = function (howMany) {
    for (var i = 0, x = 1; i < howMany; i++, x *= 2) {
        console.log(x);
    }
};
dub(20);

Demo: Fiddle


Also note as suggested by @naomik below, you need to declare all the variables used in the loop as local scoped, else in can modify a variable with the same in another scope(like global or closure scope)

还要注意下面的@naomik所建议的,你需要将循环中使用的所有变量声明为本地作用域,否则可以在另一个作用域中修改相同的变量(如全局或闭包范围)

#2


Seems like you've overcomplicated the for loop:

好像你的for循环过于复杂:

function dub(howMany) {
    for(var i = 1, y = 1;i<=howMany;i++, y+=y) {
        console.log(y);
    }
}

I'm not clear on the increment/decrement portion of the for loop or why you assigned an empty string to z in the initialization. The reason your loop was returning duplicates is found by walking through the loop with the variable states in mind.

我不清楚for循环的递增/递减部分或为什么在初始化中为z分配了一个空字符串。你的循环返回重复项的原因可以通过循环遍历变量状态来找到。

Iteration 01 - i:1 x:0 y:1 z: <empty string>
Iteration 02 - i:1 x:2 y:2 z: 1
Iteration 03 - i:2 x:2 y:2 z: 2
Iteration 04 - i:2 x:4 y:4 z: 3
Iteration 05 - i:4 x:4 y:4 z: 4
Iteration 06 - i:4 x:8 y:8 z: 5
Iteration 07 - i:8 x:8 y:8 z: 6
Iteration 08 - i:8 x:16 y:16 z: 7
Iteration 09 - i:16 x:16 y:16 z: 8
Iteration 10 - i:16 x:32 y:32 z: 9

Note that the way your back-assigning y to i then x to y and so forth is unnecessary. Hopefully I've answered your question as perhaps you had other requirements not shared that required chaining those values like that, but not just for creating a doubling loop as described.

请注意,您将y分配给i然后x到y等的方式是不必要的。希望我已经回答了你的问题,因为你可能还有其他要求没有共享,需要链接这些值,但不仅仅是为了创建一个如上所述的双循环。

Here's a working JSFiddle: https://jsfiddle.net/f39jd848/

这是一个有效的JSFiddle:https://jsfiddle.net/f39jd848/

#3


Never mind all these answers showing how to write the code more simply. To answer your question, here's the order of operations that gives you two console logs of the same thing.

没关系所有这些答案显示如何更简单地编写代码。要回答你的问题,这里的操作顺序为你提供了两个相同的控制台日志。

For reference:

dub = function(howMany){
for(i = 1, y = 1, z = ''; z < howMany ; i = y, y = x, z ++ ){
    x = i * 2;
    console.log(x);
}};dub(20);

tl;dr: because the value of i is updated in the final-expression to be y BEFORE y is updated to be x (the result of the expressions in the loop). 2 iterations of the loop must pass before i equals a new value of x.

tl; dr:因为i的值在final-expression中被更新为y之前y被更新为x(循环中表达式的结果)。在i等于x的新值之前,循环的2次迭代必须通过。

Full walkthrough (hopefully no errors, I'm tired)

完整的演练(希望没有错误,我很累)

  1. During the first execution of this loop, none of the final-expressions (after the second ;) are evaluated. i equals 1, so x is assigned the value 2 (1*2).
  2. 在第一次执行此循环期间,不会计算最终表达式(在第二个;之后)。 i等于1,因此x被赋值为2(1 * 2)。

  3. The final-expressions are evaluated. i is assigned the value y which is 1 (the same value that i had on the first loop). y is assigned the value x, which is 2.
  4. 评估最终表达式。 i被赋值为y,即1(与第一个循环相同的值)。 y被赋值为x,即2。

  5. The contents of the for loop are evaluated again. Since i has been assigned 1 again, the result is the same, and 2 is logged again (and x equals 2 again.
  6. 再次评估for循环的内容。由于我再次被分配1,结果是相同的,并且再次记录2(并且x再次等于2)。

  7. The final-expressions are evaluated. i is assigned the value of y (now 2 from step 2), and y is assigned the value of x ( still 2 from step 2 ).
  8. 评估最终表达式。 i被赋值为y(现在是步骤2中的2),y被赋值为x(步骤2中仍为2)。

  9. the loop is executed again... i now contains the value 2 so x is assigned the value 4 and 4 is logged to the console.
  10. 循环再次执行...我现在包含值2,因此x被赋值为4,并且4被记录到控制台。

  11. final-expressions: i is assigned the value of y (still 2), then y is assigned the value of x (now 4).
  12. final-expressions:i被赋值为y(仍为2),然后y被赋值为x(现在为4)。

  13. This pattern continues...
  14. 这种模式继续......

#4


If you put this at the top of your for loop, it'll skip the second iteration:

如果你将它放在for循环的顶部,它将跳过第二次迭代:

if (z%2==1) continue;