DEV Community

Discussion on: Daily Challenge #265 - Equal Sides

Collapse
dry profile image
Hayden Mankin

Javascript

const equalSides = (arr) => {
  let right = arr.reduce((a, b) => a + b);
  let left = 0;
  return arr.findIndex(i => {
    right -= i;
    if (left == right) {
      return true;
    }
    left += i;
    return false;
  });
};

console.log(equalSides([1, 2, 3, 4, 3, 2, 1])); // 3
console.log(equalSides([1, 100 , 50, -51 , 1 , 1])); // 1
console.log(equalSides([20, 10, 30, 10, 10, 15, 35])); // 3
console.log(equalSides([-8505, -5130, 1926, -9026])); // -1