title: How to store the result of each iteration of a for loop into an array (Javascript)
body: <p>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</p>
  
  <p>So I am doing Problem 1 of Project Euler.</p>
  
  <p>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)...</p>
  
  <p>Anyways.</p>
  
  <p>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.</p>
  
  <p>I'm sure this sounds kind of confusing so let me paint my picture w/ an example...</p>
  
  <p>I have a for-loop:</p>
  
  <pre><code>for (i = 1; i &lt;= 3; i++) {
  var x = 0;
  x += (i * 3);
  WHAT DO I DO NEXT????
  </code></pre>
  
  <p>^ 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)</p>
  
  <p>1st iteration: </p>
  
  <pre><code>i = 1; x = 0
  </code></pre>
  
  <p>Original equation...</p>
  
  <pre><code>(i * 3) + x            
  </code></pre>
  
  <p>So...</p>
  
  <pre><code>(1 * 3) + (x = 0) = 3
  </code></pre>
  
  <p>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?)</p>
  
  <p>2nd iteration of loop:</p>
  
  <pre><code>i = 2; x = 3
  
  (2 * 3) + (x = 3) = 9     
  </code></pre>
  
  <p>(same question as before: how would I add this value to an array?)</p>
  
  <p>3rd iteration:</p>
  
  <pre><code>i = 3; x = 9
  
  (3 * 3) + (x = 9) = 18      
  </code></pre>
  
  <p>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</p>
  
  <p>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 <code>3 + 6 + 9</code>), x's value was 9???</p>
  
  <p>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?...</p>
  
  <pre><code>for (i = 1; i &lt;= 3; i++) {
  var array = [];
  x = 0;
  x += (i *3); 
  array.push(x);
  };
  </code></pre>
  
  <p>^ 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 <em>after each iteration of the for loop</em>??? And I'm not seeing what's wrong with my for-loop to where it's returning a value of x as 9? </p>
  
  <p>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....</p>
  
  <p>THANKS IN ADVANCE...</p>