title: Splitting array into equal halves
body: <p>Problem: find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.</p>
  <p>My solution</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function findEvenIndex(arr) {
    var sum = i =&gt; i.reduce((a, b) =&gt; a + b),
      l = arr.length;
  
    for (let j = 0; j &lt;= l; j++) {
      if (sum(arr.slice(0, j - 1)) === sum(arr.slice(j, l))) {
        return j
      } else {
        continue;
      }
    }
    return -1
  
  }
  console.log(
    findEvenIndex([1, 2, 3, 4, 3, 2, 1])
  )</code></pre>
  </div>
  </div>
  </p>
  <p>When I run this on say findEvenIndex([1,2,3,4,3,2,1]), It doesn't return anything? Where is the error that prevents 3 from being returned in the case of this example?</p>
  <p>I've set the for loop procedure as follows to see what's going on</p>
  <pre><code>  for(let j = 0; j &lt;= arr.length; j++){
      var left = arr.slice(0, j-1), right = arr.slice(j)
      console.log(left, right)
    } 
  /* returns 
  [1] [3,4,3,2,1] 
  [1,2] [4,3,2,1] 
  [1,2,3] [3,2,1]
  as expected
  */
  </code></pre>
  <p>However, when try to console.log the sum of these arrays:</p>
  <pre><code>function sum(i){ return i.reduce((a, b) =&gt; a+b)} 
      
      var l = arr.length;
    
    for(let j = 0; j &lt;= l; j++){
      var left = arr.slice(0, j-1), right = arr.slice(j)
      console.log(sum(left), sum(right))
    }
  </code></pre>
  <p>Using the snippet above, findEvenIndex([1,2,3,4,3,2,1]) returns &quot;15 16&quot;?</p>