Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. const reducer = (sum, currentValue) => sum + currentValue;
  2.  
  3. function findEvenIndex(array) {
  4. if (array && array.length < 1) {
  5. return -1;
  6. }
  7. for (let i = 0; i < array.length; i++) {
  8. const left = [];
  9. const right = [];
  10. let leftSum = 0;
  11. let rightSum = 0;
  12. //make an array of all items on the left of i (excluding i)
  13. left.push(...array.slice(0, i));
  14. //make an array of all items on the right of i (excluding i)
  15. right.push(...array.slice(i + 1, array.length));
  16. if(left.length >= 1){
  17. leftSum = left.reduce(reducer)
  18. }
  19. if(right.length >= 1){
  20. rightSum = right.reduce(reducer);
  21. }
  22. if(leftSum == rightSum){
  23. return i;
  24. }
  25. }
  26. return -1;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement