Javascript Array rangeSum(start, end)

Description

Javascript Array rangeSum(start, end)


function findEvenIndex(arr)
{
  for (let i = 0; i < arr.length; i++) {
    let leftSum = arr.rangeSum(0, i);
    let rightSum = arr.rangeSum(i + 1, arr.length);
    //w w w.  j a  v  a  2 s  .com
    if (leftSum === rightSum) {
      return i;
    }
  }
  
  return -1;
}

Array.prototype.rangeSum = function(start, end) {
  if (start === end) {
    return;
  }
  return this.slice(start, end).reduce((a, b) => a + b);
}



PreviousNext

Related