filterEvens
Given an array of numbers, return a new array containing only the even numbers. Use the `filter` method.
Examples
filterEvens([1, 2, 3, 4, 5, 6]) → [2, 4, 6]
filterEvens([10, 15, 20, 25]) → [10, 20]
Starter code
function filterEvens(arr) {
}
Complexity of the optimal solution
Time O(n), space O(n). The `filter` method iterates through all 'n' elements of the array. It creates a new array for the results, which can be up to size 'n' in the worst case. This leads to linear time and space complexity.