Cant implement map on a prototype

Tell us what’s happening:
Hi again guys! I ve been working on this challenge for a while and im a bit infuriated honestly lol. I already passed the test but im trying to do less for loops so i still want to make this work.
I dont know why this returns undefined. I ve checked this inside the push method and it still equals to the array myMap is being called on (in this case s).


// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback) {
var newArray = [];
// Only change code below this line
newArray.push(this.forEach(element => callback(element)))
// Only change code above this line
return newArray;
};

var new_s = s.myMap(function(item) {
return item * 2;
});
console.log(new_s)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36.

Challenge: Implement map on a Prototype

Link to the challenge:

newArray.push(this.forEach(element => callback(element)))

What do you think that is going to do? What do you think the forEach returns? Get in the habit of checking the docs when you aren’t sure.

1 Like

You can make this work, but you have the this.push in the wrong spot.

1 Like