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;
});
// 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;
});
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.
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.