Implement the filter Method on a Prototype | HELP [SOLVED]

Implement the filter Method on a Prototype | HELP [SOLVED]
0

#1

So i’m kinda new to functional programming and having a little hard time getting my head around. I can use the inbuilt higher order methods but when this challenge asks to implement a prototype version of filter() method .i am kinda stuck here. Can some one help me out figuring out this one. So basically i understand what filter method does but still having hard time with this one.


#2

See if you can first write out (in plain language) how the filter method works. Once you have written out the steps the filter method uses, then you should be able to attempt writing the code for it.

Hint: You have actually implemented the map method instead of the filter method. The filter method only should return elements where the function evaluates to true.

Note: It is much better if you can copy/paste the code into a forum post instead of a screenshot, because we can take your code and test it out in order to give you better advice.

To 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

thanks for clearing up the topic .i literally sat down and wrote down the whole thing about how filter method works. and i am pretty sure i understood it. So the callback method should evaluate to true in order to filter method work right. something like this :

if(callback(this[i])===true)

whole code :

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

#4

hey thank you so much for the advice i thought about it for like 1 hour . and finally completed the challenge. :slight_smile: I was making a small mistake on the previous method now i finally understood it and fixed it .This is the code that actually worked at least.

Array.prototype.myFilter = function(callback){
  var newArray = [];

  for(let i=0;i<this.length;i++){
    if(callback(this[i])===true){
      newArray.push(this[i]);
    }
  }
  return newArray;

#5

I was wondering why my code doesn’t work. I tried the basic same thing but with a for/in statement

Array.prototype.myFilter = function(callback){
  var newArray = [];
  for (let item in this) {
    if (callback(item) === true)
      newArray.push(item);
  }
  return newArray;
};

#6

The solution below worked me. When you pushed ‘item’ to the ‘newArray’ you were pushing the property name assigned to it. Using ‘this[item]’ will get you access to the value.

Hope this helps!

for (let item in this) {
if (callback(this[item]) === true)
newArray.push(this[item]);
}


#7

This is the shortest code i managed, one line :slight_smile:

Array.prototype.myFilter = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach(el => callback(el) ? newArray.push(el) : '');
  // Add your code above this line
  return newArray;
}

#8

Man its taking me quite a bit of practice to truly understand and grasp “this” during these exercises. Thanks for the solution. I had something similar but was stuck. I kept writing
newArray.push(callback(this[i]));


#9

If u wanna another way, this is my solution:

this.forEach( (el) => callback(el) && newArray.push(el) );

#11

@Sai This is really nice! I’m having some difficulty understanding why it works, though. Why is everything after && executed? I googled around and couldn’t find this special syntax anywhere. Could you please explain?


#12

@dahdah I believe this link will help you.
Sr because of my late respond.


#13

@Sai That’s fascinating, I never realized operators could be used in that way. The link was super helpful, thanks!


#14

@dahdah You’re welcome! :smiley:


#15

using normal for loop and without the confusing this . However , ofc this code only works for s variable .
if u want the function to work for anything . u would have to use ‘this’.

  for(let i = 0; i < s.length; i++) {
    if(callback(s[i])) {
      newArray.push(s[i]);
    }
  }