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.
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.
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. 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;
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]);
}
This is the shortest code i managed, one line
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;
}
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]));
If u wanna another way, this is my solution:
this.forEach( (el) => callback(el) && newArray.push(el) );
@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?
@6in That’s fascinating, I never realized operators could be used in that way. The link was super helpful, thanks!
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