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}`);
newArray.push(callback(this[i]));
}
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?
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: