Functional Programming: Implement the filter Method on a Prototype

Functional Programming: Implement the filter Method on a Prototype
0

#1

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

#2

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.


#3

What returns a function?


#4

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.


#5

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.


#6

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?


#7

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


#8

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


#9

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


#10

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


#11

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

#12

@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.


#13

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


#14

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.


#15

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


#16

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.


#17

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


#18

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.


#19

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