JSFiddle - React, Tailwind, and code Playground

Codility test 9 June 2017

by Matthew Day

JavaScript

sampleArray =[-1,3,-4,5,1,-6,2,1]

function add(a, b) {
	return a + b;
}

function solution(A) {
	var length = A.length;
  var add =   function(a, b) {
    return a + b;
  }
  
	A.map((input, index) => {
  	const pre = A.slice(0, index + 1).reduce(add, 0);
    const post = A.slice(index, length).reduce(add, 0);
    if(pre === post) {
    	console.log(`Index ${index} has equilibrium.`);
    	return index;
    } else {
    	console.log(`Index ${index} fails.`);
    	return -1;
    }
  });
}

solution(sampleArray);

// Source
// https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers