Implement map on a Prototype - Problem in "For ... in" loop

Tell us what’s happening:

Here is the solution I came up with (witch is not working of course)

Your code so far

var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
  var newArray = [];
  for (let i in this) {
    console.log(`i = ${i} // new_s[i] = ${callback(this[i])}  // This = ${this}`);
  return newArray;
var new_s = s.myMap((item) => item * 2);

I looked at the given solution, and I think I understand that’s exactly what I did, except that I used for (let i in this) instead of this.forEach()

So I wrote the console.log that you can see and it returns:

I was taking for granted that here ‘this’ would refer to the content of the array, but it looks like somehow the loop is iterating avor the array values then the prototype object to find the myMap property? :thinking:

That’s why I checked ‘this’ in the console.log, but wherever I put the test I can’t find a ‘this’ containing anything else than the expected values of the array.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:

Don’t use loops on arrays, it is for plain objects and even then you need to be careful with it. You are correct, it is iterating over all the properties.


Thank you, I should have read the for in documentation.
I replaced it with for (let i = 0; i < this.length; i++) {} and it’s working fine