Implement custom Array.prototype.filter
Implement a custom Array.prototype.myFilter(callback) method that creates a new array containing the elements that pass the test in callback.
Your implementation should behave like this simplified version of Array.prototype.filter:
- Iterate from index
0toarray.length - 1 - For each element, call
callback(currentValue, index, array) - If
callback(...)returns a truthy value, includecurrentValuein the result - Do not mutate the original array
Constraints
- Do not use the built-in
Array.prototype.filter.
Example
const numbers = [1, 2, 3, 4, 5];
const filtered = numbers.myFilter((number) => {
return number > 3;
});
console.log(filtered); // Output: [4, 5]