Seeeyonnn
Seeeyonnn

Reputation: 325

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

So I am doing Problem 1 of Project Euler.

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 i.e. 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.

I'm sure this sounds kind of confusing so let me paint my picture w/ an example...

I have a for-loop:

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. 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)

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??? (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?)

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 some reason, when I run the code, the final value of x is 9, and not 18

So, basically I am trying to add the sum of the 3 values...But what do I do next? 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???

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??? And I'm not seeing what's wrong with my for-loop to where it's returning a value of x as 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...

Upvotes: 1

Views: 32675

Answers (3)

Hedayat H
Hedayat H

Reputation: 382

You can use like this: and you have to define X out of the loop

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

Upvotes: 0

StackSlave
StackSlave

Reputation: 10627

You are declaring var x = 0 and var array = [] at every step of the loop, try something like:

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

Upvotes: 0

p.s.w.g
p.s.w.g

Reputation: 149020

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;
}

Upvotes: 9

Related Questions