Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:
Describe your issue in detail here.
i couldn’t complete my third test given on this segment. please help me out

Your code so far

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
this.forEach(a=> {
    if(callback(a)){
      newArray.push(a)
    }
  })
  // Only change code above this line
  return newArray;
};
console.log([23, 65, 98, 5, 13].myFilter(item => item % 2))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement the filter Method on a Prototype

Link to the challenge:

Look back at the previous Challenge - the filter callback can have more than 1 argument!

1 Like

Ya, this one is a tricky one. Look at the third test:

[1, 1, 2, 5, 2].myFilter((element, index, array) => array.indexOf(element) === index) should return [1, 2, 5]

How many arguments are being passed to the callback function? That’s how many you need to pass it when you call the callback function in your code.

You might want to review the forEach method as well. You’ll need something extra from that in order to solve this. (Hint: The callback function for the forEach method can take more than one argument).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.