Functional Programming - Implement the filter Method on a Prototype

Remember, it’s important to show all of your code. You get different results based upon when you put those three lines.

Idk I’m also stuck on this

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button 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.

Hi @anon5045811

If you have a question about a specific challenge as it relates to your written code for that challenge need some help, click the Ask for Help button. This button 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.

When i console.log it, I got [] back

what could that mean?

also, just for completion, the functions from the other two tests

 console.log(["naomi", "quincy", "camperbot"].myFilter(element => element === "naomi")) // should return `["naomi"]`.

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

What is the code? Show your full code when you talk a about something happening

That nothing is being returned to newArray

Please show the code when talking about what happens

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
for (let i = 0; i < this.length; i++) {
  if (this[i], i, this === callback) {
    newArray.push(callback(this[i], i, this));
  }
}
//console.log(this);
//console.log(newArray);
//console.log(this[i]);
//console.log(i);

  // Only change code above this line
  return newArray;
};
console.log([23, 65, 98, 5, 13].myFilter(item => item % 2)); 

comma operator means that only the last part of the expression gives a value, this === callback.
What is this comparison?

I need to compare all of these, so I listed them

you need to use callback by calling it to check your condition

2 Likes

These console logs can’t work here because of scope

yeah, I noticed… Sorry

Do i do this ? :

Array.prototype.myFilter = function(callback) {
  const newArray = [];
  // Only change code below this line
for (let i = 0; i < this.length; i++) {
  if (callback) {
    newArray.push(callback(this[i], i, this));
  }
}
//console.log(this);
//console.log(newArray);


  // Only change code above this line
  return newArray;
};
console.log([23, 65, 98, 5, 13].myFilter(item => item % 2)); 

I really don’t get how to call it to check my condition…

Repeat for me what the filter function does

filter checks if each item of the array meet the condition and pushed to the new array only the ones that do

and the condition is the callback function you pass to it right?
so how do you call a function? I’m sure you did this many times in your past

Like this: callback()