Equal Sides Of An Array

JavaScript

function findEvenIndex(arr) {
  for (const index in arr) {
    let workingArr = [...arr];
    workingArr[index] = 0;

    const firstHalf = workingArr.slice(0, index);
    const secondHalf = workingArr.slice(index);

    const firstHalfSum = firstHalf.reduce((acc, num) => acc + num, 0);
    const secondHalfSum = secondHalf.reduce((acc, num) => acc + num, 0);

    if (firstHalfSum === secondHalfSum) return parseInt(index);
  }

  return -1;
}

Tags

  1. javascript (Private)
  2. 6-kyu (Private)
  3. codewars (Private)
  4. answer (Private)