Advertisement
dabidabidesh

Equal Sums

Jun 6th, 2020
2,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //06.Equal Sums
  2. function equalSums(arr) {
  3.  
  4.   for (let i = 0; i < arr.length; i++)
  5.     if (isEqual(arr, i)) {
  6.       console.log(i)
  7.       return
  8.     }
  9.   console.log('no')
  10.  
  11.   function isEqual(arr, index) {
  12.     let leftSum = 0
  13.     let rightSum = 0
  14.     for (let i = 0; i < index; i++)
  15.       leftSum += arr[i]
  16.     for (let i = index + 1; i < arr.length; i++)
  17.       rightSum += arr[i]
  18.  
  19.     if (leftSum === rightSum)
  20.       return true
  21.     else
  22.       return false
  23.   }
  24. }
  25. equalSums0([1, 2, 3, 3])
  26.  
  27. const update = function (x, y) {
  28.   // Do something with x and y
  29. };
  30.  
  31. function equalSums0(arr) {
  32.   'use strict'
  33.  
  34.   const isEqual0 = function (arr, index) {
  35.     let leftSum = 0
  36.     let rightSum = 0
  37.     for (let i = 0; i < index; i++)
  38.       leftSum += arr[i]
  39.     for (let i = index + 1; i < arr.length; i++)
  40.       rightSum += arr[i]
  41.  
  42.     return (leftSum === rightSum) ? true : false
  43.   }
  44.  
  45.   for (let i = 0; i < arr.length; i++)
  46.     if (isEqual0(arr, i)) {
  47.       console.log(i)
  48.       return
  49.     }
  50.   console.log('no')
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement