// the global Array
var s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback){ //??? how do I read this line, is array.prototype the
var newArray = []; //global array
// Add your code below this line
s.forEach(item => {
if (callback(item)) newArray.push((item)) //??? here I'm taking the anonymous function parameter
// and in the if statement setting it as a function that takes item
}); //as parameter(why whats happening) and pushing that item
// to the newArray
// Add your code above this line
return newArray;
};
var new_s = s.myFilter(function(item){ //??? here I'm creating a new variable and assigning the function
return item % 2 === 1; //to it and returning the item that has a remainder of 1 (unequal number)
});
I solved this code but how do I read this code. I’m struggling to understand how this code it working. can anyone please explain it in a clearer way, I’ve put three question marks in the comments at the code that I don’t understand.
Functional Programming: Implement the filter Method on a Prototype