Implement a Generic Filter Function

Problem Implement your own generic, reusable filter function in JavaScript, mirroring Array.prototype.filter: it takes a predicate callback and returns a new array containing the elements for which the predicate returns truthy.

Requirements

  • myFilter(array, predicate, thisArg?) -> Array (or Array.prototype.myFilter = function(predicate, thisArg) {...}).
  • Predicate invoked as predicate(element, index, array) — all three arguments.
  • Returns a new array; the input is never mutated.
  • Truthy (not strictly true) return values include the element.
  • Skips holes in sparse arrays.

Areas to design

  • The iteration contract (all three predicate arguments, thisArg, truthiness).
  • Correct sparse-array / holes handling and length snapshotting.
  • Prototype extension vs standalone function, and edge/error cases.
asked …
LeaderboardSalaryAccount