Skip to content

Instantly share code, notes, and snippets.

Created July 31, 2016 19:49
Show Gist options
  • Save anonymous/b21f877eb67a1b3bf508263609a6d7bd to your computer and use it in GitHub Desktop.
Save anonymous/b21f877eb67a1b3bf508263609a6d7bd to your computer and use it in GitHub Desktop.
https://repl.it/CgPh/12 created by sethopia
// Attach a method to the Array prototype called myFilter.
// It should be utilized and perform exactly as Array.prototype.filter does. Use the code snippet below as a guide.
Array.prototype.myFilter = function(func) {
var newArr = [];
for (var i = 0; i < this.length; i++) {
if(func(this[i],i,this)) {
newArr.push(this[i]);
}
}
return newArr;
};
function isEven(num) {
return num % 2 === 0;
}
var arr = [0,1,2,3,4,5,6];
var filtered = arr.myFilter(isEven);
console.log(filtered); // should be [ 0, 2, 4, 6 ]
Native Browser JavaScript
>>> [ 0, 2, 4, 6 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment