Tell us what’s happening:
I don’t know what’s wrong in this code. It has thrown an error.
Thanks for any help !
Your code so far
// The global variable
var s = [23, 65, 98, 5];
Array.prototype.myFilter = function(callback){
// Only change code below this line
var newArray = callback.filter(item => item % 2 === 1);
// Only change code above this line
return newArray;
};
var new_s = s.myFilter(function(item){
return item % 2 === 1;
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36.
Challenge: Implement the filter Method on a Prototype
const myArray = [1, 2, 3, 4];
// How to use a function
myFunction(myArray);
// How to use a method
myArray.myMethod(myCallbackFunction);
Obviously, the above code will not do anything, but you can see the difference between calling a function, and passing an argument to the function, and using a method on an object, and passing a callback function to the method.