-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathequal-sides-of-an-array.js
More file actions
25 lines (22 loc) · 835 Bytes
/
equal-sides-of-an-array.js
File metadata and controls
25 lines (22 loc) · 835 Bytes
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
// {1,2,3,4,3,2,1}
function findEvenIndex(arr) {
const sumSubArray = (arr, start, end) => {
let sum = 0;
for (let index = start; index < end; index++) {
sum += arr[index];
}
return sum;
};
for (let index = 0; index < arr.length; index++) {
const leftSum = sumSubArray(arr, 0, index);
const rightSum = sumSubArray(arr, index + 1, arr.length);
if (leftSum == rightSum) {
return index;
}
}
return -1;
}
console.log(findEvenIndex([1, 2, 3, 4, 3, 2, 1]), 3, "The array was: [1,2,3,4,3,2,1] \n");
console.log(findEvenIndex([1, 100, 50, -51, 1, 1]), 1, "The array was: [1,100,50,-51,1,1] \n");
console.log(findEvenIndex([1, 2, 3, 4, 5, 6]), -1, "The array was: [1,2,3,4,5,6] \n");
console.log(findEvenIndex([20, 10, 30, 10, 10, 15, 35]), 3, "The array was: [20,10,30,10,10,15,35] \n");