Functional Programming - Implement the filter Method on a Prototype

hi can someone show me how to change

Array.prototype.myFilter = function(callback) {
  const newArray = [];

  for(let i=0;i<this.length;i++){
if(Boolean(callback(this[i],i,this)) === true){
newArray.push(this[i]);
}

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

to use a forEach instead?

i keep getting unexpected token errors;

Please also post the code with the errors.

here it is, i cannot find the appropriate way to use the foreach

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
this.forEach((element in this) => {
  if(Boolean(callback(this[i],i,this))){
    newArray.push(this[i]);
  }
})
  // Only change code above this line
  return newArray;
};

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