Advertisement
Spocoman

06. Equal Sums

Jan 22nd, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.  
  3.     let index = 0;
  4.     let isEqual = false;
  5.  
  6.     for(let i = 0; i < input.length; i++){
  7.         let leftSum = input.slice(0, --i).reduce((a, b) => a + b, 0);
  8.         let rightSum = input.slice(++i).reduce((a, b) => a + b, 0);
  9.         if(leftSum === rightSum){
  10.             index = --i;
  11.             isEqual = true;
  12.             break;
  13.         }
  14.     }
  15.     console.log(input.length === 1 || isEqual ? index : "no");
  16. }
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement