Functional Programming: Implement the filter Method on a Prototype

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;
});
1 Like

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.

markdown_Forums

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.

1 Like

If I type in

a = 3

in a repl.it, 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. repl.it 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.

1 Like

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.

1 Like

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

1 Like

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.

1 Like

You can still use a traditional for loop instead of using a forEach

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

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

@EBA Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, please give hints and suggestions instead of full working solutions.

Thank you.

1 Like

Can someone explain to me what’s going on here? I didn’t understand the task on this one.

Do you understand how Array.prototype.filter works? If so, then you are trying to recreate this method by using a for loop or Array.prototype.forEach by creating another Array.prototype method named myFilter.

I thought I did but now I’m not sure.

I used the loop when trying to do it, but didn’t add (callback(s[i])) {…}. That was the part that I didn’t understand

The normal filter method uses a callback function as the 1st parameter. The callback function normally has 1 to 3 arguments, with the 1st argument representing the current element being iterated over. For this challenge, you only have to use this one argument, which is why the function being passed has one argument (item) as seen below:

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

If you call normal filter method on the s array with the same callback function, you would write:

var s = [23, 65, 98, 5];
console.log(s.filter(function(item){
  return item % 2 === 1;
}); // displays [23, 65, 5]

Your custom myFilter method must produce the same result if called on the s array. You will use the the loop of your choice (for loop or forEach method) to iterate through the array and create a new array with values returned by calling the callback function with each array element’s value as the argument of the callback function.

You did just enough to pass the tests but you didn’t actually implement a .Filter() Method.

I was confused by trying to simply push each number in s onto newArray instead of actually filtering each number. After solving this, I think this might help:

You’re trying to return the numbers in s that, when divided by 2, have a remainder of 1 (basically odd numbers). Once you run though each number in s using forEach or a for loop, you need to check IF the number % 2 === 1. If that’s the case, then you can push the number onto newArray.

Hope this helps.

Just try this. It works for me.

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach(i => {
    if (callback(i)){
      newArray.push(callback(i));
    }    
  });
  console.log(newArray);
  // Add your code above this line
  return newArray;
};

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

Array.prototype.myFilter = function(callback){
var newArray = [];
// Add your code below this line
for(let prop in this){
if(callback(this[prop])){
newArray.push(this[prop])
}
}
// Add your code above this line
return newArray;

};

var new_s = s.myFilter(function(item){
return item % 2 === 1;
});
// is there anything wrong with this, in this use case?