Implement map on a Prototype (Help please)

Tell us what’s happening:

I feel about 70% confident in what I’m doing below, but I also have a lot of doubt in that confidence and really need someone to shoot a hole in it because I can’t see past it right now. I’ve left my comments related to my logic in my code below.

Your code so far

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

Array.prototype.myMap = function(callback){

//I must create a new array because I need something to manipulate besides the global variable per the necessary logic of FP.
 var newArray = [];

  // As THIS takes on the property of the object in its scope, I use THIS as copy of the callback.  I use it to loop through the array and apply the logic of CALLBACK.
for(var i = 0; i < this.length; i++){
//I return newArray with the array callback was called upon within it.

  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/70.0.3538.110 Safari/537.36.

Link to the challenge:

DARN close. One thing is missing: when you push this[i] into newArray, that would be the time to implement your callback. Can you work out how you might do that?

for (let i=0; i< this.length; i++){
  newArray.push(/** do the callback stuff here, using this[i] as a param **/)

But, rather than give you everything, how might you implement that callback?

Thanks @snowmonkey. It was a lot easier than I thought it would be for some reason. However, I have another query in regards to the solution.

I thought that .push() would not take more than one parameter. I got a lot of such-and-such is not a function replies when I was going through various attempts to solve and it seems .push() would fail.

So question:

  1. Push seemed to me to not to have functional behavior like map() or filter() and to be something else, like a lower tier function (not as powerful). Is this a faulty assumption and, if so, is .push() - although it manipulates content - of the same valence as map() or filter()?

Also, one other query. I want to make sure I understand the place of this in the above code.

this is a keyword that copies the content of parameter callback and carries that into the manipulating section of the code and, thereby, blocks the global myMap from mutation. Is that a correct understanding of it?

push serves a single function: to add an element to the end of an array. As such, yes – it takes a single parameter. The thing you wish to push. That thing could be an array, a string, a number, or an object – or the result of some sort of action.

In this case, we are push() -ing the results of calling callback(this[i]), which means “take the current value from the array, and run it through my callback function – when that returns, push whatever value it has returned onto my array.” Nothing more.

So your callback (in this case) returns a number. We simply push that number onto that new array.

I don’t think it has the same “valence” as filter() or map(), no. '.push()is not a higher-order function in the sense that.filter(), but your IMPLEMENTATION` is, in fact, a higher-order function – it calls some function on every iteration of that array, and does something with them, then returns the new array.

So push() is not doing the work. You are. :wink:

1 Like

I thought that .push() would not take more than one parameter.

When in doubt, check the docs. Here they are for push. MDN should be something you are consulting often.

Push seemed to me to not to have functional behavior like map() or filter() and to be something else, like a lower tier function (not as powerful).

I wouldn’t say it that way. These are all methods (functions built into an object/array). map and filter take callbacks and do iterative behavior. I don’t know that that makes them more powerful, per se. I guess you could argue that they “do more” and could build case that that is a definition of more powerful.

I just like to think of them as different tools. Is my calculator “more powerful” than my stapler? I guess you could say that. But if I need to bind some papers, the stapler is suddenly more important to me. They’re just different tools for different jobs.

1 Like

Ok, so this is a special variable within javascript. It represents the current scope or context in which the function is being run. It is one of the most confusing bits of javascript, and has caused pattern baldness in many developers because of rampant hair-pulling.

In your particular case, this represents the current instance of the Array object. When you create a new array, it is wrapped in this Array object, and inherits all the functionality of that object – including the function you’ve just attached to the Array.prototype. So, when you reference this in your function, you’re saying “the current array”.