Functional Programming - Implement map on a Prototype

I do not understand the solution provided in the guide. Specifically:


      newArray.push(callback(this[i], i, this));

According to MDN the Array.prototype.push() adds an element, or elements to the end of the array. The solution has 3 arguments in the push statement, which suggests, that it is adding 3 elements to the new array. but, it is only adding 1 element. What am I missing here???

Your browser information:

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

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

Not quite.

This callback has 3 arguments

OH! OK, I get that now, those are arguments to the callback. Thanks for enlightening me.

BUUUUT, how is one to know how many arguments are needed for the callback function given the instructions?

Also, what happens to the additional arguments when they are not given, such as the first two tests???

[23, 65, 98, 5, 13].myMap(item => item * 2) should equal [46, 130, 196, 10, 26].

["naomi", "quincy", "camperbot"].myMap(element => element.toUpperCase()) should return ["NAOMI", "QUINCY", "CAMPERBOT"].

The parameters are always in the same order (element, index, array) and the callback is always passed all three arguments whether you use them or not.

If you use one parameter, the callback has access to the element.

If you use two parameters, the callback has access to the element and the index.

If you use three parameters, the callback has access to the element, the index, and the original array it was called on.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.