Sum of Odd and Even Numbers

Published by Matt 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

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

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

sum_odd_and_even([0, 0]) ➞ [0, 0])

Notes

Count 0 as an even number.

Watch a quick demo on how Edabit works.