Implement a map protoype... why doesn't my code work?

Implement a map protoype... why doesn't my code work?
0.0 0

#1

Hi everyone.

I wrote the following code for this challenge:

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

Array.prototype.myMap = function(callback){
  var newArray = [];
  // Add your code below this line
  this.forEach(function(item){
    newArray.push(callback(item));
  // Add your code above this line
  return newArray;
  })
};

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

every console.log at every point gives out the right results but for some reason I keep getting undefined when trying to log new_s. Where is my error?

Thanks in advance!


#2

Double check the location of your return statement.


#3

Oh right!! Thanks!! :slight_smile: