Created
May 30, 2018 03:57
-
-
Save 0D1NTR33/45e0cb7a6be1345179563b8f0abfb712 to your computer and use it in GitHub Desktop.
Equal Sides Of An Array
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
// awesome | |
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; | |
} | |
// my | |
function findEvenIndex(arr) | |
{ | |
for (var i = 0; i < arr.length; i++) { | |
var res_left = arr.reduce(function(sum, current, index) { | |
return (index < i) ? (sum + current) : sum; | |
}, 0); | |
var res_right = arr.reduceRight(function(sum, current, index) { | |
return (index > i) ? (sum + current) : sum; | |
}, 0); | |
if (res_left == res_right) { | |
return i; | |
} | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment