JavaScript Codility Demo Solution

The biggest problem with your solution is that nested loop.

You iterate over the whole array at each index to calculate the sum of left and right parts at current index. One of their requirements is O(n) complexity, while yours is O(n^2) (i think).

You only need to loop over the array twice: once to get the sum of elements and once to find the equilibrium. At the start of the second loop the sum on the left == 0 and the sum on the right == total. Iterating over the elements you just need to update the sums: the right sum = total - left sum - value at the current index, then you compare if right == left and if not - the left sum grows by the value at current index.

I've scored 100pts for this one:

function solution(A) {
  var total = (function(a){ var l = a.length, s = 0; while(--l>-1){ s+=a[l] } return s; }(A)), 
  eq = -1, 
  l = A.length, 
  Lsum = 0, 
  Rsum = 0; 
  A.forEach(function(n,i){ 
    Rsum = total - Lsum - n; 
    if(Rsum == Lsum){ eq = i; /* in fact no need to continue, should terminate here. */ } 
    Lsum += n; 
  }); 
  return eq;
}