I used every solution and then this is the solution that was posted but I have an error:
For this example [1,1,2,5,2] this method didn’t work. But why?
The answer should be 1,2,5 but my answer is 1,1,5.
Your code so far
var s = [1,1,2,5,2];
Array.prototype.myFilter = function(callback) {
const newArray = [];
// Only change code below this line
for (const i of this) {
if (callback(i)) newArray.push(i);
}
// Only change code above this line
return newArray;
};
var new_s = s.myFilter(function(item){
return item % 2 === 1;
});
console.log(new_s); //print [1,1,2]
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36
Challenge: Functional Programming - Implement the filter Method on a Prototype
Link to the challenge: