Implement map on a Prototype new

Implement map on a Prototype new


Tell us what’s happening:

Your code so far

// the global Array
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  newArray = => {return val});
  // Add your code above this line
  return newArray;


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


Your browser information:

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

Link to the challenge:


Lots of things wrong here, so just a few to get you going:

This line doesn’t make sense:

newArray = => {return val})

If this was the actual map function in the JS standard library, then it would work, although all that’s happening is you’re making a copy of the array (for each value in s, return that value). But you’re writing your own version. So when it gets to that line, your entire function gets called again and so on and so on, recursively.

Inside the function, you need to use this. You are creating a function that gets attached to all objects of the kind Array. this refers to the context something is being called from. The map function you are writing is called on an array, so that array is going to be the context, so that array is this.

The point of functions is to generalise things. But your function is tied to a [global] variable called s which references an array that only exists in that bit of code. The function is supposed to work with any array. At the minute, you function, regardless of what you pass in, is dependent on that array. And if I copy the code and try to run it locally without the example array included there, it’ll naturally break (even if the rest of your code is right), because s doesn’t exist for me.

You are supposed to take the array (this), and for each element in it, run the function given to map on that element, and push the result of that into a new array.