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

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

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

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?

Thought this recursive solution was cool:

Array.prototype.myFilter = function(callback, arr = [], i = 0){
  return callback(this[i]) ? this.myFilter(callback, arr.concat(this[i]), i + 1) : i < this.length ? this.myFilter(callback, arr, i + 1) : arr;
};