Functional Programming: Implement map on a Prototype confusion

Tell us what’s happening:
Can someone maybe clarify this challenge? If I understand correctly, I am trying to basically create the map() function by doing a traditional forloop. However when I console.log the condition in the for loop, I have no output. What am I doing wrong?

My thought process was to iterate through whatever is passed through the callback argument and push the result into newArray. But, I must be incorrect?

Your code so far


// The global variable
var s = [23, 65, 98, 5];

Array.prototype.myMap = function(callback){
var newArray = [];
// Only change code below this line
for(let i =0; i < callback.length; i++){
if(callback > 0){
  console.log(callback); 
  newArray.push(callback[i]); 
}
}
// Only change 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/84.0.4147.125 Safari/537.36.

Challenge: Implement map on a Prototype

Link to the challenge:

see here, this is where the method is called

callback is the method parameter, and it is a function

here you are trying to use the function length and also check if the function is greater than 0 - is that what you want to check?

in methods like this, you can reference the array on which the method is used as this

so

in the above, this get value of s and callback of function(item){return item * 2;}

1 Like

Understood. Thank you. I was utilizing callback incorrectly. That helps a lot.