Solving for equilibrium index in an array
I'm piggy backing on @jonah's answer. The first code snippet returns what Codility is asking for: any equilibrium index. Second code snippet returns all answers like in @jonah's suggestion. I also added two accumulating arrays for side by side comparison. You can add a breakpoint at the end of the function to compare the two arrays.



var A = [-1, 3, -4, 5, 1, -6, 2, 1];

function solution(A) {        
    var rightSum = A.reduce((acc, val) =&gt; acc + val, 0), leftSum = 0;
    
    for (var i = 0; i &lt; A.length; i++) {
        
        rightSum -= A[i];

        // Codility is asking for any, so return first found
        if (rightSum === leftSum) return i; 
        
        leftSum += A[i];
    }
    
    return -1;
}

solution(A);






var A = [-1,3,-4,5,1,-6,2,1];

function solution(A) {
    
    var rightSum = A.reduce((acc, val) =&gt; acc + val, 0), leftSum = 0, rightSums = [], leftSums = [], answers = [];
    
    for (var i = 0; i &lt; A.length; i++) {
        
        rightSum -= A[i];
        
        // You can keep track of answers for comparison/debugging
        rightSums.push(rightSum);
        leftSums.push(leftSum);
        
        if (rightSum === leftSum) answers.push(i);
        
        leftSum += A[i];
        
    }
    
    return answers;
}

solution(A);