Implement the filter Method on a Prototype Help

Implement the filter Method on a Prototype Help
0.0 0

#1

Tell us what’s happening:
Hi, can someone help to clarify my doubt. I am doing this practice and I am confused.

For the function(callback) why is it when you want to use the function you use callback(), I thought the name of the function should be outside of the parentheses?

For example
function square(number){

return number*number
};

then when you call you use console.log(square(3)) for example.

Or is the callback is a function by itself?

Your code so far


// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line
  
  // Add your code above this line
  return newArray;

};

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-the-filter-method-on-a-prototype


#2

A callback function is a function that is passed to another function.

In the above example you are creating a myFilter function which takes a callback function as a parameter. In this function example you are passing an anonymous function without a name. You could have defined it as well, for example:

myCallback = function(item){
  return item % 2 === 1;
}

var new_s = s.myFilter(myCallback);

In these cases you are actually passing a variable which is a function. Including the parenthesis, trigger the actual function. That is why there are no parenthesis when passing it as a variable, so you can use it later within the function WITH parenthesis. That would be the part that the excercise asks you to do.

I hope this helped you?


#3

Thank you so much for your explanation.

It helps to clarify some of my doubts. But I am still quite confused.

So this code below

myCallback = function(item){
return item % 2 === 1;
}

var new_s = s.myFilter(myCallback);

By using (myCallback) it is triggering the function of returning all the object in the parameter of item that has a remainder of 1?


#4

No it is passing the function to myFilter.

myCallback = function(item){
return item % 2 === 1;
}

is just a regular function. You can call it by doing myCallback(5) which will return true or myCallback(4) which will return false.

Array.prototype.myFilter = function(callback){
};

is a regular function as well which has no content at the moment so it will not return anything. myFilter(5) will return undefined. myFilter(myCallback), will return undefined as well.

now if you pass a callback function such as myCallback, then you can trigger it within myFilter.

For example:

Array.prototype.myFilter = function(callback){
return callback(5);
};

now the function myFilter(myCallback) will return true.

In short, this means that myFilter will take any function (the callback function) and execute it with parameter 5. For the current challenge, this means that you can use this technique to create a loop and run the callback function multiple times with different arguments.