Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:
I get this error:

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

should return

[1, 2, 5]

and it does not!
Can anyone help me?

Your code so far

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line

  // Only change code above this line
  return newArray;
};

Your browser information:

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

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

Link to the challenge:

hi there, what code did you write for this challenge? (i don’t see anything written above?)

Array.prototype.myFilter = function(callback, arr = [], i = 0){
  if(i < this.length){
    if(callback(this[i])){
      arr.push(this[i])
    }
    return this.myFilter(callback,arr, i+1)
  }else{
    return arr
  }
}

please re-write the code so only the lines between the comments are changed.

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line

  // Only change code above this line
  return newArray;
};

If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

A post was split to a new topic: Implement the Filter Method on a Prototype

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