-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
23 lines (19 loc) · 679 Bytes
/
filter.js
File metadata and controls
23 lines (19 loc) · 679 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
*
* @param {Array} arr array of elements
* @param {Function} fn function that checks if the element satisfy the condition
* @returns {Array} a new array with the elements that satisfy the condition
*/
function filter(arr, condition) {
if (arr.length === 0) return []
const x = arr[0]
const xs = arr.slice(1)
return (condition(x) ? [x] : []).concat(filter(xs, condition))
}
const greaterThanTwo = (e) => e > 2
console.log(filter([1,2,3], greaterThanTwo))
console.log(filter([2], greaterThanTwo))
console.log(filter([], greaterThanTwo))
console.log(filter([-1, 2, 44], greaterThanTwo))
const isTrue = (e) => e
console.log(filter([true, true, false], isTrue))