Functional Programming - Implement the filter Method on a Prototype

Tell us what’s happening:

I don’t know how to check if conditions are met. Please, help.

Your code so far

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 === true) {
    newArray.push(callback(this[i], i, this));
  }
}
console.log(this);
console.log(newArray);
  // 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/124.0.0.0 Safari/537.36

Challenge Information:

Functional Programming - Implement the filter Method on a Prototype

what is the condition that has to be met?

all of these:
[23, 65, 98, 5, 13].myFilter(item => item % 2) should equal [23, 65, 5, 13].

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

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

I need to make sure that only items who pass these get pushed into newArray. Don’t know if this makes sense but I don’t think i can explain it any better.

Not the tests, I ask you what is the condition you mention here

I want to check which items in the array meet the conditions in callback function

so the callback function is the condition, right?
So you need to pass the array item to it and check the boolean value it outputs.

Do you need to change the array items in any way here?

because you are pushing the output of the callback to a new array, it doesn’t look what we want to do

Yes, i only want newArray to include items that pass the condition. But right now it pushes the entire array to newArray.

use the condition to check which to push then!

Do you have anything in your toolbox that execute conditionally?

if and filter. But I can’t use filter and when i try with if, it doesn’t do a thing

if seems promising. What is your code with if?

The one I linked at the top.

you need to check your condition, where is your condition?

my condition is callback.

if (callback(this[i], i, this) === true) {
newArray.push(callback(this[i], i, this));
}

Like that, maybe?

and what is it that you need to push to the new array?
now the total sum of your code is that you push true

i need to push this[i], i, this if === true

what are these three things that you are pushing?
what happens to the output if you do that?

element, index, the array itself.
It should be updated

so you want in the output array the item, the index of the item in the old array and the old array itself, for each item.
Test your output by printing it

it doesn’t show anything when console.log it. No number or anything at all.
I did it like this :

console.log(this[i]);
console.log(this);
console.log(i);

no, console the output, like for example

console.log([23, 65, 98, 5, 13].myFilter(item => item % 2)) // should equal [23, 65, 5, 13]

also remember you need to call your function for it to execute

1 Like