Plz help where is the mistake

Tell us what’s happening:

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
  for (let i=0;i<Array.length;i++){
    newArray.push(callback(Array[i]))
  }
  // Add your code above this line
  return newArray;

};

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 5.1; rv:52.0) Gecko/20100101 Firefox/52.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype

You need to double it, right? 23->46, 65->130, 98->196 and 5->10? It works, try adding console.log(new_arr1); below everything and look at console for array results.

1 Like

You need to iterate over var s so in your for loop you should have s.length, then pass s[i] to the callback.

Put console.log(newArray); before the return to check the results.

1 Like

You are retrieving the .length property from the Array prototype object. That’s not what you want to do, you want it from the object where the function is called from.

1 Like

Hi @mahmod84,

@dariushine has the correct answer. Don’t look at the Array object, you need to look at the object that is calling the myMap function. You want s.myMap(...), not Array.myMap(...), to return a new array to new_s. You want the object “before dot”, the one used to call the method (https://javascript.info/object-methods).

1 Like

I don’t see the OP using this in their code. My answer works. Could you explain further?

The boilerplate code already has that:

var new_s = s.myMap(...)
1 Like

Hi @eoja,

In op’s function expression, he has:

Array.prototype.myMap = function(callback){
  var newArray = [];
  for (let i=0;i<Array.length;i++){
    newArray.push(callback(Array[i]))
  }
  return newArray;
};

You’ll see he uses Array.length and Array[i], which isn’t declared anywhere. He needs to replace it with this.length and this[i] so that the calling object is referenced when myMap is called.

In his example, he had var new_s = s.myMap(...). The myMap(...) needs to know what array to reference. this tells the myMap to apply the callback function to the calling array object (in this case, the s array). But since he used Array, the .myMap does not know how to access the calling array object; instead it’s trying to access Array, which isn’t defined anywhere. It’s as if he used Array.myMap(...). Even if he did [1, 2, 3].myMap(...), [8, 84, 4, 8, 2, 3].myMap(...), etc., it will always look at the Array variable rather than the calling array object.

If you go into your chrome devtool console, copy and paste his code, and try to call it with this example: [1, 2, 3].myMap(num => num * 2), you’ll get [NaN].

If you change his code just a bit to:

Array.prototype.myMap = function(callback){
  var newArray = [];
  for (let i=0;i<this.length;i++){
    newArray.push(callback(this[i]))
  }
  return newArray;
};

Copy and paste into the devtools console, and try [1, 2, 3].myMap(num => num * 2) again, you’ll get [2, 4, 6]. The this lets .myMap access the information in the calling [1, 2, 3] object.

Ref: https://javascript.info/object-methods#this-in-methods

2 Likes

@MrBanh Thanks for explaining. I was using s.length and s[i] but that does not make myMap reusable. I knew there had to be a way to make it reusable, and that is what using this does. It was a good opportunity for me to have a refresher on this.

1 Like