Last active
December 30, 2019 19:46
-
-
Save risingBirdSong/5f00e2b05a10f2a9fbbdd682655a8a39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findEvenIndex(arr) | |
{ | |
for(var i=1; i<arr.length-1; i++) { | |
if(arr.slice(0, i).reduce((a, b) => a+b) === arr.slice(i+1).reduce((a, b) => a+b)) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
function findEvenIndex(arr) | |
{ | |
var left = 0, right = arr.reduce(function(pv, cv) { return pv + cv; }, 0); | |
for(var i = 0; i < arr.length; i++) { | |
if(i > 0) left += arr[i-1]; | |
right -= arr[i]; | |
if(left == right) return i; | |
} | |
return -1; | |
} | |
const sum = (a, from, to) => a.slice(from, to).reduce((a, b) => a + b, 0) | |
const findEvenIndex = a => a.findIndex((el, i) => sum(a, 0, i) === sum(a, i + 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A solution from other warriors that i like better than my solution...