Functional Programming - Implement map on a Prototype

Tell us what’s happening:

I’ve no idea what I have to do here.These are three different function. Please help

Your code so far

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line

  // Only change code above this line
  return newArray;
};

Your browser information:

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

Challenge Information:

Functional Programming - Implement map on a Prototype

You need to use the callback function itself instead of coding for what the callback function might be.

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
function add(arr) {
  console.log(newArray.forEach((item) => {item * 2}));
}

  // Only change code above this line
  return newArray;
};
console.log([23, 65, 98, 5, 13].myMap(item => item * 2));



here’s what I have so far. What do you think? Am i going in the right direction?
I don’t get waht you meant by using the callback function itself. How can i use it if it doesn’t exist? I’m very confused.

What do you mean by “it doesn’t exist”?

How would you use any other function arguments normally? callback exists in exactly the same way as any other function arguments

I mean i have to write this function first

But to write this function you need to use the argument callback

If you have this function

function add(a, b) {
 return a + b;
}

The arguments a and b are ‘real’ and get used

So, function callback()?

No, you don’t need to define the callback, callback is a function argument.

my function’s argument needs to be callback?

Right here, callback is defined as an argument to the myMap. It’s already there. You need to use it.

i don’t understand how

How do you call any function? callback is a function so you use it the same way you use any function

callback(), then?

Yep, though you will need to pass arguments to the callback function call

callback(this.forEach((item) => {item * 2});

Hmm, I’m not sure that’s the arguments this callback will accept. And forEach doesn’t have a return value, that doesn’t really make sense there.

What, big picture, are you trying to do in the Step?

Solve this:

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

That’s not what I asked though. What, in human words, is the goal of this function you are supposed to write?

It should work the same way map() does

Ok, what does map do?