Sum of Even Numbers
easySave
ArrayMath
Given an array of integers, return the sum of all even numbers in the array.
Example 1
Input
[ 2, 5, 3, 10, 4 ]
Output
16
Explanation. Sum of even numbers 2, 10, and 4 is 16.
Example 2
Input
[ 17, 23, 71 ]
Output
0
Explanation. There are no even numbers in the input array.
Example 3
Input
[ -2, -4, -6 ]
Output
-12
Explanation. Sum of even numbers -2, -4, and -6 is -12.
Example 4
Input
[ 0, 1, 2, 3, 4 ]
Output
6
Explanation. Sum of even numbers 0, 2, and 4 is 6.
Follow-up: Can you solve this problem in a more efficient way using a functional programming style in JavaScript?
Constraints:
The input array may contain zero, positive, and negative numbers.
- Views
- 2