← Back to challenges

Filter Items from an Array

JavaScriptHardarrayslanguage_fundamentals

Instructions

Create a function that takes two parameters:

  1. An array with items ranging from numbers and strings.
  2. An array with items ranging from numbers, strings and and anonymous function.

The function should return only the items from the first array that satisfies the anonymous function present in the second array.

Detail

#filterArray([1, 2, 3, 4], [100, (num) => num % 2 === 0]) ➞ [2, 4]

# 2 and 4 from [1, 2, 3, 4] satisifies  anonymous function in the second array.
# (num) => num % 2 === 0          2 % 2 === 0 and 4 % 2 === 0
# 1 and 3 do not satisfy (num) => num % 2 === 0

Examples

 filterArray([1, 2, 3, 4], [1, 2, (num) => num % 2 === 0]) ➞ [2 , 4]

 filterArray([1, 2, 3, 4, 5], [1, 2, (num) => num % 2 === 1,"eon","epoch"]) ➞ [1, 3, 5]

 filterArray(["apple", "kiwi"], [2, (txt) => txt.indexOf("a") > -1]) ➞ ["apple"]

Notes

  • Inputs are always two arrays.
  • There is always only one anonymous function in the second array that needs to be checked with the item in the first array.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.