Fxnl Programming: Implement the filter Method on a Prototype

Please explain this code line by line.
.myFilter is equal to a function so why call “function(item)”?
Is “function(item)” going to into where is says “callback” above so
it would be: "Array.prototype.myFilter=function(function(item)){…?
If this is correct, then what is the function being called inside the function?
I am confused.
Is “Callback” equal to “function(item)”?
In the “for” loop, what is “this” equal to?
“callback” is not a function but a placeholder or variable.
Why are we doing “Callback(this[i] === true)”?
Recommendations to help me understand this section of Functional Programming?
It seems like it switched to another language once I started this section.
I have done fine until I hit this section and am not understanding any section nor
any solution provided for this Functional Programming section.

I did a tende and it has issues working for me most times to separate out the code.

let s = [23,65,98,5];

Array.prototype.myFilter = function(callback) {
  var newArray = [];
  // Add your code below this line
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i]) === true) {
      newArray.push(this[i]);
    }
  }
  // Add your code above this line
  return newArray;
};

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

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The function(item){...} you see there? Is used, inside .myFunction(...), as the callback parameter. Within your custom filter function, you are passing in a function, which the filter uses on each member of the array to determine whether or it’s kept or discarded.

This is an example of a higher-order function, in that it expects to get passed a function that it will use internally, without needing to know anything about that function. The only requirement for that function is that, for each member, give me a true or false value.

1 Like