Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
    this.forEach((a, b, c) => {
      if(callback(a, b, c)){
        newArray.push(callback(a, b, c))
    }
    });
  // 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/116.0.0.0 Safari/537.36

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

Link to the challenge:

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!


push what now?

This doesn’t seem right to me. What values are you supposed to be putting in newArray.

Tell us what’s happening:
I was able to do it but after doing so , I tried to do it again. so I think the problem is with the format of my function

Your code so far

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
    this.forEach((a, b, c) => 
      if(Boolean(callback(a, b, c))){
        newArray.push(a)
      }
    )
  // 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/116.0.0.0 Safari/537.36

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

Link to the challenge:

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

this passes but I want to re do it with the use of forEach()

If i was you, I would double check the brackets.
good rule of thumb is to always put content(or expression or whatever you want to call them) of functions within {} , even for arrow functions, if it is more than a single-line statement, i would argue that it is better to use them even for single line statements, but that is my personal opinion.

I would also check for redundancies.

why are there a ‘b’ and a ‘c’ parameters if you never use them?
also is there really a need for Boolean(whatever) inside if statement?
those won’t give you errors but the latter is just useless typing and the former is confusing to read, to me at least

They are being used, when calling the callback function:

if(Boolean(callback(a, b, c))){

you are right, what got me confused was that in all the exercise tests but the last, the ‘callback’ only had one single parameter and I did not check them all.
after double-checking I found out that the last test had a callback with 3 parameters.
So they are in fact necessary.
so that was my bad :slight_smile: