简体   繁体   English

如何将for循环的每次迭代结果存储到数组中(Javascript)

[英]How to store the result of each iteration of a for loop into an array (Javascript)

This question could also be (depending on how you look at it) - How to use a for loop to do a basic math operation that adds up the sum of the multiples of 3 这个问题也可能是(取决于您的看法)-如何使用for循环进行基本的数学运算,将3的倍数之和相加

So I am doing Problem 1 of Project Euler. 所以我正在做Euler项目的问题1。

I've (already) hit a steel-reinforced wall. 我已经(已经)撞上了钢筋墙。 I could use some help - with just a specific portion of the problem please (I am not looking for just an answer to the problem, but rather, an answer to my specific problem so I can carry on with trying to solve the remainder problem using any answer you guys provide me here)... 我可以使用一些帮助-请仅解决问题的一部分(我不是在寻找问题的答案,而是在解决我的具体问题,因此我可以继续尝试使用解决问题你们在这里给我的任何答案)...

Anyways. 无论如何。

I (think I) created a for loop that will give me the multiples of 3. However, I'm trying to store the result of each iteration of the for-loop so that it adds up to the sum of those multiples ie I'm trying to store the result from each iteration of the loop - whether it be into an array or into a variable that takes the sum of the multiples - it doesn't matter to me - I wouldn't mind learning both methods. 我(认为我)创建了一个for循环,该循环将为我提供3的倍数。但是,我正在尝试存储for循环的每次迭代结果,以便将它们的总和乘以这些总和。 m尝试存储循环的每次迭代的结果-无论是将其存储到数组中还是将变量乘以乘数的总和-对我而言都无关紧要-我不介意学习这两种方法。

I'm sure this sounds kind of confusing so let me paint my picture w/ an example... 我确定这听起来有点令人困惑,所以让我用一个例子来画我的画...

I have a for-loop: 我有一个for循环:

for (i = 1; i <= 3; i++) {
var x = 0;
x += (i * 3);
WHAT DO I DO NEXT????

^ So I would think that this gives me x with a value of 3 upon the 1st iteration of the loop, x with a value of 9 on the 2nd loop, and x with a value of 18 on the final loop. ^因此,我认为这在循环的第一次迭代中给我x值为3,在第二个循环中给x提供9的值,在最终循环中给x提供18的值。 That's correct, right? 没错吧? (if this were returning 18 I don't think I would need to store the values of each iteration into an array) (如果返回18,我认为不需要将每次迭代的值存储到数组中)

1st iteration: 第一次迭代:

i = 1; x = 0

Original equation... 原始方程式...

(i * 3) + x            

So... 所以...

(1 * 3) + (x = 0) = 3

So after completion of the 1st loop, x has a value of 3, right??? 因此,在完成第一个循环后,x的值为3,对吗? (Question: How would I store this value of x (which is 3) - how would I store it in an array while in this stage of the for loop?) (问题:如何存储x的值(为3)-在for循环的此阶段如何将其存储在数组中?)

2nd iteration of loop: 循环的第二次迭代:

i = 2; x = 3

(2 * 3) + (x = 3) = 9     

(same question as before: how would I add this value to an array?) (与之前相同的问题:如何将这个值添加到数组中?)

3rd iteration: 第三次迭代:

i = 3; x = 9

(3 * 3) + (x = 9) = 18      

Q: shouldn't this be the final value of x upon completion of the for loop??? 问:这不应该是for循环完成后x的最终值吗??? For some reason, when I run the code, the final value of x is 9, and not 18 由于某种原因,当我运行代码时,x的最终值为9,而不是18

So, basically I am trying to add the sum of the 3 values...But what do I do next? 因此,基本上我正在尝试将3个值的和相加...但是接下来我该怎么办? I thought my for loop would add the result of the equation after each loop to x, but instead of ending up w/ 18 (the sum of 3 + 6 + 9 ), x's value was 9??? 我以为我的for循环会将每个循环后的方程式结果加到x,但x的值为9而不是以w / 18( 3 + 6 + 9的总和)结束。

Should I use an array? 我应该使用数组吗? If so, I'm thinking I could add the return value to an array, but I'm not sure how to add the result of each iteration of the loop to an array. 如果是这样,我想我可以将返回值添加到数组中,但是我不确定如何将循环的每次迭代的结果添加到数组中。 Maybe the following?... 也许以下?

for (i = 1; i <= 3; i++) {
var array = [];
x = 0;
x += (i *3); 
array.push(x);
};

^ I tried running that in jfiddle, but it would only add the last value of x (9) into the array... So how would I add the value of x to an array after each iteration of the for loop ??? ^我尝试在jfiddle中运行它,但是只会将x(9)的最后一个值添加到数组中...因此,在for循环的每次迭代之后,如何将x的值添加到数组中? And I'm not seeing what's wrong with my for-loop to where it's returning a value of x as 9? 而且我看不到for循环在哪里返回x的值为9的问题?

Also, I'm assuming the euler problems get significantly more difficult as we progress? 另外,我假设随着我们的进步,欧拉问题变得更加困难了? If so, I've got a TON of work/practice to do.... 如果是这样,我还有很多工作要做。

THANKS IN ADVANCE... 提前致谢...

Just create the array once, and push the result at each iteration to the array: 只需创建一次数组,然后将每次迭代的结果推送到数组:

var array = [];
for (i = 1; i <= 3; i++) {
    var x = 0;
    x += (i *3); 
    array.push(x);
}

Or for that matter, just use this: 或为此,只需使用以下命令:

var array = [];
for (i = 1; i <= 3; i++) {
    array.push(i *3);
}

Or to simply get the sum of the factors, use this: 或仅获取因素的总和,请使用以下方法:

var x = 0;
for (i = 1; i <= 3; i++) {
    x += i *3;
}

You are declaring var x = 0 and var array = [] at every step of the loop, try something like: 您在循环的每一步都声明var x = 0var array = [] ,请尝试以下操作:

var array = [], x = 0;
for (i=1; i<4; i++){
  x += (i*3); 
  array.push(x);
}

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

相关问题 如何在 javascript 中的 for 循环的每次迭代中将一个数组插入另一个数组 - How to insert an array to another array at each iteration of a for loop in javascript jquery / javascript-如何遍历数组并在每次迭代中创建变量? - jquery/javascript -how to loop through array and create variables in each iteration? 如何将循环的每个迭代存储为innerHTML - How to store each iteration of a loop as innerHTML javascript循环的每次迭代中,如何显示每个随机数的结果? - How do I display the result of each random number in each iteration of the loop in javascript? 循环行为的JavaScript每次迭代输出相同的结果 - Javascript for loop behaviour outputting the same result each iteration 如何遍历HTTP请求循环并将每个迭代的数据适当地存储到数组中 - How to iterate through a loop of HTTP requests and store the data of each iteration appropriately to an array 如何将函数存储到数组中并遍历JavaScript中的每个函数 - How to store functions into an array and loop through each in javascript 在 for 循环的每次迭代中输出一个数组 - Outputting an array each iteration of a for loop 将$ .each结果存储到数组中? - store $.each result into array? CasperJS for loop每次迭代都会产生相同的结果 - CasperJS for loop produces the same result for each iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM