Sum of Odd and Even Numbers

Published by Mubashir Hassan in

Write a function that takes an array of numbers and returns an array with two elements:

  1. The first element should be the sum of all even numbers in the array.
  2. The second element should be the sum of all odd numbers in the array.

Example

sumOddAndEven([1, 2, 3, 4, 5, 6]) ➞ [12, 9]
// 2 + 4 + 6 = 12 and 1 + 3 + 5 = 9

sumOddAndEven([-1, -2, -3, -4, -5, -6]) ➞ [-12, -9])

sumOddAndEven([0, 0]) ➞ [0, 0])

Notes

Count 0 as an even number.

Watch a quick demo on how Edabit works.