Advertisement
igelov

06. Equal Sums

Jun 6th, 2020
3,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(array) {
  2.     function equalSum(array) {
  3.         if(array.length===1){
  4.             return 0;
  5.         }
  6.         let right_sum = 0;
  7.         let left_sum = 0;
  8.         for (let i = 1; i < array.length; i++) {
  9.             right_sum += array[i];
  10.         }
  11.         for (let    i = 0, j = 1; j < array.length; i++, j++) {
  12.             right_sum -= array[j];
  13.             left_sum += array[i];
  14.             if (left_sum === right_sum) {
  15.                 return i+1;
  16.             }
  17.         }
  18.         return "no";
  19.     }
  20.  
  21.     console.log(equalSum(array));
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement