Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const reducer = (sum, currentValue) => sum + currentValue;
- function findEvenIndex(array) {
- if (array && array.length < 1) {
- return -1;
- }
- for (let i = 0; i < array.length; i++) {
- const left = [];
- const right = [];
- let leftSum = 0;
- let rightSum = 0;
- //make an array of all items on the left of i (excluding i)
- left.push(...array.slice(0, i));
- //make an array of all items on the right of i (excluding i)
- right.push(...array.slice(i + 1, array.length));
- if(left.length >= 1){
- leftSum = left.reduce(reducer)
- }
- if(right.length >= 1){
- rightSum = right.reduce(reducer);
- }
- if(leftSum == rightSum){
- return i;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement