Tell us what’s happening:
Describe your issue in detail here.
This code does work but I do not fully understand it. I understand it until I get to the last challenge which states.
[1, 1, 2, 5, 2].myFilter((element, index, array) => array.indexOf(element) === index)
should return [1, 2, 5]
.
I do not understand why this[i], i, this is the correct parameters to pass in order to make it run correctly.
Your code so far
Array.prototype.myFilter = function(callback) {
const newArray = [];
// Only change code below this line
for (let i = 0; i < this.length; i++) {
if(callback(this[i], i, this))
newArray.push(this[i])
}
// Only change code above this line
return newArray;
};
console.log([1, 1, 2, 5, 2].myFilter((element, index, array) => array.indexOf(element) === index))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
Challenge: Functional Programming - Implement the filter Method on a Prototype
Link to the challenge: