Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:
I am using a for each loop. I even looked at the solution, none of which use a for each loop. Even looking at the solution, I am a little confused on what this challenge wants. I console logged out what I currently have, and I am getting back what is expected. What part is missing?

Your code so far
Array.prototype.myFilter = function(callback) {
const newArray = ;
// Only change code below this line
this.forEach(item => callback(item)?newArray.push(item): null
);

// Only change code above this line
console.log("this: ", this)
console.log("callback: ", callback());
console.log("newArray: ", newArray);
return newArray;
};

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
  this.forEach((item)=>{
    console.log("callback2", callback(this[i]))
    
  });
  console.log(newArray)

  // Only change code above this line
  console.log("this: ", this)
  console.log("callback: ", callback());
  console.log("newArray: ", newArray);
  return newArray;
};

Your browser information:

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

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

Link to the challenge:

Console log won’t put anything in your newArray.

Also, a ternary isn’t a replacement for an if statement.

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