Write Your Own Map and Filter

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write myMap(items, fn) and myFilter(items, fn) without using the built-in map or filter. Both call fn(item, index, items) once for each item, in order. myMap returns a new array of fn's results. myFilter returns a new array of the items for which fn returned a truthy value. Both always return a new array, even when nothing changes, and neither changes items. The arrays in the checks have no gaps.

Your code must pass

  • myMap returns each item's result, in order
  • myFilter keeps the items fn accepts, in order
  • Both pass the item, its index and the array to fn
  • Both return a new array and leave the input alone
  • Works without the built-in map or filter