Functional Programming - Implement map on a Prototype

Im hoping for some clarification on what this challenge is asking me to do.

**THE TASK: Write your own Array.prototype.myMap(), which should behave exactly like You should not use the built-in map method. The Array instance can be accessed in the myMap method using this.

I think it is asking me to write a for loop that is equivalent to this line of code:

Array.prototype.myMap = function(callback) {
  const newArray = [];

Is that correct?

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

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
for (let i = 0; i < s.length; i++) {
  // Only change code above this line
  return newArray;

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

They aren’t specifically asking for a for.
But they said

It is recommended you use a for loop or Array.prototype.forEach() .

However you can ignore this if you wish.

So just use one of those 2 methodologies that is functionally identical to this specific line of code, correct?

Array.prototype.myMap = function(callback) {
  const newArray = [];

No. That line is a partial function definition. You are trying to fill in the function definition so that it replicates the behavior of .map

It wants you to loop over this inside the myMap function which is the array the method was called on (this in a method invocation).

Then pass in each array element to the callback function.

It might be helpful to not inline everything and add a few logs

function myMap(callback) {
  console.log('"this" is ->', this);
  console.log('"callback" is ->', callback.toString());
  this.forEach((item) => {
    console.log('"callback(item)" is ->', callback(item));

Array.prototype.myMap = myMap;

function callbackFunction(item) {
  return item * 2;

const new_s = s.myMap(callbackFunction);
1 Like

Thank you, this really clarified my confusion for me

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