Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 19, 2020 06:50
Show Gist options
  • Save hassan-maavan/9c642405df062775ddb2e052d5f1a7ee to your computer and use it in GitHub Desktop.
Save hassan-maavan/9c642405df062775ddb2e052d5f1a7ee to your computer and use it in GitHub Desktop.
You are going to be given an array of integers. Your job is to take that array and 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. https://www.codewars.com/kata/5679aa472b8f57fb8c000047
function findEvenIndex(arr)
{
let i;
for(i = 0; i < arr.length; i++){
let slic1 = arr.slice(0,i).reduce((num1, num2) => num1 + num2, 0);
let slic2 = arr.slice((i + 1), arr.length).reduce((num1, num2) => num1 + num2, 0);
if(slic1 == slic2){
return i;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment