-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequalSides.js
32 lines (25 loc) · 844 Bytes
/
equalSides.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Equal Sides Of An Array
// 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.
function findEvenIndex(arr) {
// Function to find sum of array
function reduction(ray) {
return ray.reduce((prev, current) => prev += current)
}
for (let i = 0; i < arr.length - 1; i++) {
// left of i
let left = arr.slice(0, i);
// right of i
let right = arr.slice(i + 1);
if (reduction(arr.slice(1)) === 0) {
return 0;
}
if (reduction(left) == reduction(right)){
console.log(left, right);
return i;
}
}
return -1;
}