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

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.

1 Like

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;

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 Likes

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

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

1 Like

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

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

3 Likes

If u wanna another way, this is my solution:

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

@6in 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?

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

3 Likes

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

@dahdah You’re welcome! :smiley:

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]);
    }
  }
Spoiler Code
// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myFilter = function(callback){
var newArray = [];
// Add your code below this line
for(var i = 0; i < this.length; i++){
newArray.push(callback(this[i]))
}

// Add your code above this line
console.log(newArray, s, new_s, callback, this)
return newArray;

};

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

Please, someone can explain me whats wrong with my code, he is returning [ true, true, false, true ], not the values that pass the test in the callback

thanks

You made function that push boolean.
Simple, you said:
Push inside newArray if value%2 === 1 or not.
Result of this is boolean.

I love how you gave your code a persona :joy::joy::joy: