Functional Programming: Implement the filter Method on a Prototype (Order of sequence question and input question)

Hello!

So I’m a bit lost on this problem. Not sure if overthinking it or not thinking enough.

One of the questions that I have, is function(callback) happening after function(item) {return item…} occurs? And does that mean that new_s will be equal to what ever newArray is equal to? Or will var new_s be equal to whatever return items returns?

To break down what I’m exactly asking, is function(item){return item} happening first and then function(callback) or is function(callback) happening first and then function(item){return item} occuring?

I’m really stuck with what I should exactly be inserting into newArray as well because all my outcomes aren’t right.

Sorry if its too much. Thanks!

Your code so far


// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback){
// Only change code below this line

const newArray =[];
for (let i=0; i< this.length;i++){
  newArray.push(s[i]);
}
callback();
// Only change code above this line
return newArray;
};

var new_s = s.myFilter(function(item){
return item % 2 === 1;
});

console.log(new_s);

Your browser information:

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

Challenge: Implement the filter Method on a Prototype

Link to the challenge:

this is the value of callback in this case

you are never using it
you are also not doing any filtering
the callback returns a Boolean value when it is called with a number as argument

you need to use it to check which elements to keep and which not, instead you are not filtering and keeping all elements of the starting array

also to keep your code reusable you can’t do s[i], as s is a global variable, you need to keep referencing this even there

1 Like

Oh I see!!! Thank you so much!! That makes callback make much more sense. Thank you!