Saw the answer, still don't understand :(

Tell us what’s happening:
I don’t understand the answer. Could anyone help me?

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
  this.forEach(a => newArray.push(callback(a)));
  // Add your code above this line
  return newArray;

};

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

It’s much easier to help you if we can see the code that you wrote when you were trying to solve the problem.

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

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  for(let i=0;i<s.length;i++)
  {
    newArray.push(callback);
  }
  // Add your code above this line
  return newArray;

};

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

Give me the link to the challenge please.

It might be failing because you are missing:

this.forEach(a => newArray.push(callback(a)))

But idk as long as I don’t read the challenges.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

It looks like you were probably most of the way there, but forgot to call callback with an argument.

1 Like

Yes,but I don’t understand that, callback is an argument itself, why it used as a function?

The callback is a function. In JavaScript, functions are “first class citizens”. This means that they can be passed into a function as an argument (this is how callbacks work) or returned by functions. You’ve actually done exercises related to both of these cases, earlier in the JavaScript curriculum.

Thanks for your help.