← Back to challenges

Sum of Odd and Even Numbers

JavaScriptHardarraysdata_structureshigher_order_functionsmath

Instructions

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.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Numbers to Objects