Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //09. Left and Right Sum
- function leftAndRightSum(arg) {
- let [n, sumLeft, sumRight, diff] = [+arg[0], 0, 0, 0]
- for (let i = 1; i <= n; i++)
- sumLeft += +arg[i]
- for (let i = n + 1; i <= 2 * n; i++)
- sumRight += +arg[i]
- diff = Math.abs(sumRight - sumLeft)
- if (diff == 0)
- console.log(`Yes, sum = ${sumLeft}`)
- else
- console.log(`No, diff = ${diff}`)
- }
- function leftAndRightSum0() {
- let n = +arguments[0][0]
- let sumLeft = 0
- let sumRight = 0
- let diff = 0
- for (let i = 1; i <= n; i++)
- sumLeft += +arguments[0][i]
- for (let i = n + 1; i <= 2 * n; i++)
- sumRight += +arguments[0][i]
- diff = Math.abs(sumRight - sumLeft)
- if (diff == 0)
- console.log(`Yes, sum = ${sumLeft}`)
- else
- console.log(`No, diff = ${diff}`)
- }
- function leftAndRightSum1(arg) {
- let n = +arg.shift()
- let sumLeft = 0
- let sumRight = 0
- let diff = 0
- for (let i = 1; i <= n; i++)
- sumLeft += +arg.shift()
- for (let i = n + 1; i <= 2 * n; i++)
- sumRight += +arg.shift()
- diff = Math.abs(sumRight - sumLeft)
- if (diff == 0)
- console.log(`Yes, sum = ${sumLeft}`)
- else
- console.log(`No, diff = ${diff}`)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement