title: Variable not holding the right array while pushing into an Array
body: <p>How can I properly scope my startA variable so that it is the first array that I pushed towards my result array ?</p>
  
  <p>As you can see from my <em>output</em> results, the first array that I am pushing into my results array is actually my end array.</p>
  
  <p>Is this a variable scoping problem ? because, I have pre-define a <strong><em>startA</em></strong> variable that holds the initial start value ([1,1]), why would the output of my <strong><em>startA</em></strong> variable still be [7,7] ? Isn't this a by-value copy since its a primitive type and not a reference type?</p>
  
  <pre><code>// Tick Toward
  function tickToward(start, end) {
      var startA = start;
      var results = [];
      results.push(startA);
      for (var i = 0; i &lt; start.length; i++) {
          if (start[i] &lt; end[i] &amp;&amp; start[i + 1] &lt; end[i + 1]) {
              start[i] += 1;
              start[i + 1] += 1;
              var a = [start[i],start[i+1]];
              results.push(a);
              // console.log("Condition 1");
              i--;
          } else if (start[i] &gt; end[i] &amp;&amp; start[i + 1] &gt; end[i + 1]) {
              start[i] -= 1;
              start[i + 1] -= 1;
              var b = [start[i],start[i+1]];
              results.push(b);
              // console.log("Condition 2");
              i--;
          } else if (start[i] &gt; end[i] &amp;&amp; start[i + 1] &lt; end[i + 1]) {
              start[i] -= 1;
              start[i + 1] += 1;
              var c = [start[i],start[i+1]];
              results.push(c);
              // console.log("Condition 3");
              i--;
          } else if (start[i] &lt; end[i] &amp;&amp; start[i + 1] &gt; end[i + 1]) {
              start[i] += 1;
              start[i + 1] -= 1;
              var d = [start[i],start[i+1]];
              results.push(d);
              // console.log("Condition 4");
              i--;
          } else if (start[i] &gt; end[i] &amp;&amp; start[i + 1] == end[i + 1]) {
              start[i] -= 1;
              var e = [start[i],start[i+1]];
              results.push(e);
              // console.log("Condition 5");
              i--;
          } else if (start[i] &lt; end[i] &amp;&amp; start[i + 1] == end[i + 1]) {
              start[i] += 1;
              var f = [start[i],start[i+1]];
              results.push(f);
              // console.log("Condition 6");
              i--;
          } else if (start[i] == end[i] &amp;&amp; start[i + 1] &gt; end[i + 1]) {
              start[i + 1] -= 1;
              var g = [start[i],start[i+1]];
              results.push(g);
              // console.log("Condition 7");
              i--;
          } else if (start[i] == end[i] &amp;&amp; start[i + 1] &lt; end[i + 1]) {
              start[i + 1] += 1;
              var h = [start[i],start[i+1]];
              results.push(h);
              // console.log("Condition 8");
              i--;
          }
      } console.log(results);
  
  }
  
  
  tickToward([1, 1], [7, 7]); // Output should be [ [ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ], [ 5, 5 ], [ 6, 6 ], [ 7, 7 ] ]
  
  // However, my output is [ [ 7, 7 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ], [ 5, 5 ], [ 6, 6 ], [ 7, 7 ] ]
  </code></pre>
  
  <p><strong><em>Here is the question if anyone is interested:</em></strong>
  <a href="http://i.stack.imgur.com/5UR5T.png" rel="nofollow">http://i.stack.imgur.com/5UR5T.png</a></p>