Advertisement
dabidabidesh

leftAndRightSum09

Feb 29th, 2020
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //09. Left and Right Sum
  2. function leftAndRightSum(arg) {
  3.  
  4.   let [n, sumLeft, sumRight, diff] = [+arg[0], 0, 0, 0]
  5.  
  6.   for (let i = 1; i <= n; i++)
  7.     sumLeft += +arg[i]
  8.  
  9.   for (let i = n + 1; i <= 2 * n; i++)
  10.     sumRight += +arg[i]
  11.  
  12.   diff = Math.abs(sumRight - sumLeft)
  13.  
  14.   if (diff == 0)
  15.     console.log(`Yes, sum = ${sumLeft}`)
  16.   else
  17.     console.log(`No, diff = ${diff}`)
  18. }
  19.  
  20. function leftAndRightSum0() {
  21.  
  22.   let n = +arguments[0][0]
  23.   let sumLeft = 0
  24.   let sumRight = 0
  25.   let diff = 0
  26.  
  27.   for (let i = 1; i <= n; i++)
  28.     sumLeft += +arguments[0][i]
  29.  
  30.   for (let i = n + 1; i <= 2 * n; i++)
  31.     sumRight += +arguments[0][i]
  32.  
  33.   diff = Math.abs(sumRight - sumLeft)
  34.  
  35.   if (diff == 0)
  36.     console.log(`Yes, sum = ${sumLeft}`)
  37.   else
  38.     console.log(`No, diff = ${diff}`)
  39. }
  40.  
  41. function leftAndRightSum1(arg) {
  42.  
  43.   let n = +arg.shift()
  44.   let sumLeft = 0
  45.   let sumRight = 0
  46.   let diff = 0
  47.  
  48.   for (let i = 1; i <= n; i++)
  49.     sumLeft += +arg.shift()
  50.  
  51.   for (let i = n + 1; i <= 2 * n; i++)
  52.     sumRight += +arg.shift()
  53.  
  54.   diff = Math.abs(sumRight - sumLeft)
  55.  
  56.   if (diff == 0)
  57.     console.log(`Yes, sum = ${sumLeft}`)
  58.   else
  59.     console.log(`No, diff = ${diff}`)
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement