 <p>Let's suppose the following two arrays and their "sum":</p> <pre><code>Array 1: 1 2 3 4 5 6 7 8 9 [length = 9] Array 2: 2 4 6 8 2 4 6 [length = 7] Sum : 3 6 9 12 7 10 13 8 9 [length = 9] </code></pre> <p>Pay attention to the last two items. The sum is equal to the value of the first array because the second array doesn't contain such number of values.</p> <pre><code>Array 1: 1 2 3 4 5 6 7 8 9 Array 2: 2 4 6 8 2 4 6 ? ? </code></pre> <p>That's exactly what algorithm does:</p> <p>1) While both arrays have numbers at <code>i</code> index - sum up.</p> <pre><code>ctr : ! ! ! ! ! ! ! \|/ [ ctr = 7 (remember: 0-based indexes)] Array 1: 1 2 3 4 5 6 7 8 9 [length = 9] Array 2: 2 4 6 8 2 4 6 [length = 7] Sum : 3 6 9 12 7 10 13 </code></pre> <p>Here <code>while (ctr &lt; array1.length &amp;&amp; ctr &lt; array2.length)</code> conditions breaks at <code>ctr &lt; array2.length</code>.</p> <p>Further, the check <code>ctr == array2.length</code> returns true meaning that the Array 2 is over and we need to continue iterating through the <code>array1</code>.</p> <pre><code>for (x = ctr; x &lt; array1.length; i++) { result.push(array1[x]); } </code></pre> <p>2) While the remaining array is not over - add values from it.</p> <pre><code>x : ! ! \|/ [ x = 10] Array 1: 1 2 3 4 5 6 7 8 9 [length = 9 ] Array 2: 2 4 6 8 2 4 6 [length = 7 ] Sum : 3 6 9 12 7 10 13 8 9 </code></pre>