Implement map on a Prototype Troubles

Tell us what’s happening:
Obviously not working. I haven’t wrapped my head around the whole functions as arguments or the map() method yet :confused:

How do I get the length of the array copied from in the for loop to create the newArray?
Is that even the right question to ask?
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 < callback.length; i++) {    
    newArray[i] = callback[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 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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

Not that it matters much but the length will be 1 not 0, because the function takes one argument. Right?

// 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 < this.length; i++) {
newArray[i] = this[i];
} console.log(newArray)
// Add your code above this line
return newArray;

};

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

ok, so now in the for loop when I use this.length for the length of array, it is working as far as copying var s into the newArray, but the challenge still wont pass. Ive tried assigning callback and item the value of newArray, but I’m not grasping why its not passing into the s.myMap(function(item) below.

I believe you are missing the call to the callback, pass the array (this[i]) into the callback inside your loop.

Spoiler

newArray[i] = callback(this[i]);

Thanks lasjorg!
Though I am still struggling to understand exactly what is going on… I’ll keep at it.