Functional Programming: Implement the filter Method on a Prototype

Functional Programming: Implement the filter Method on a Prototype
0.0 0


I can’t seem to grasp this concept. Can someone explain to me why this returns a function?

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

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line 
  this.forEach(i => {
    if(callback(i)) {newArray.push(i)

  return newArray;
  // Add your code above this line

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 the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


What returns a function?


when I use myFilter(), i seem to get [function] returned, but I’m not really sure why that is.

I’m using repl to test.


If I type in

a = 3

in a, it will return “3” because it is evaluating what I have there.

If I do:

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

It returns back a function. doesn’t list the whole function so it just tells you what it is. It is one of the oddities of command line JS. Don’t worry about it.


ok thanks. But if that’s the case, then what’s wrong in my code that’s causing it not to pass the FCC test?


When I run your code, it passes. Sometimes the browser glitches. Reboot, clear the browser cache, thy a different browser, etc.


Yup. I was looking so long to find out what was wrong with my code! Thank you! it works now


What’s going on with forEach()? I thought we haven’t learned that in the course at this point.


You may not have yet, but that does not prevent you from search and learning about other functions and features available in JavaScript.