Sum of Even Numbers
easySave
Array
Given an array of integers, return the sum of all the even numbers in the array.
Example 1
Input
[ 1, 2, 3, 4, 5 ]
Output
6
Explanation. The even numbers in the array are 2 and 4. The sum of 2 and 4 is 6.
Example 2
Input
[ 10, 21, 30, 43 ]
Output
40
Explanation. The even numbers in the array are 10 and 30. The sum of 10 and 30 is 40.
Example 3
Input
[ 1, 3, 5 ]
Output
0
Explanation. There are no even numbers in the array. Therefore, the sum is 0.
Follow-up: Can you solve this problem using a functional programming approach like using `filter` and `reduce` methods?
Constraints:
The input array will not be empty and will contain at least one element. All elements will be numbers.
- Views
- 2