Functional Programming - Implement map on a Prototype

Tell us what’s happening:
Hello everyone, I have a bit of issue here. I’ve wrote the for loop version but the output of new_s is the same as s which is [23, 65, 98, 5]. not sure why?

the value of newArray.push(this[i]); is [23, 65, 98, 5]

I think this code should work just fine, but it’s not. I’m not sure what I’m missing. please enlight me.
Your code so far

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

Array.prototype.myMap = function(callback) {
  const newArray = [];
  // Only change code below this line
  for (let i = 0; i< this.length; i++){
    newArray.push(this[i]);
  }
  // Only change code above this line
  return newArray;
};

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

console.log(new_s);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Functional Programming - Implement map on a Prototype

Link to the challenge:

thankies, with your hint, I change

newArray.push(this[i]);

to

newArray.push(callback(this[i]));

and it works.
I’m not quite sure why this[i] needs to be inside callback().
my best guess is because callback(this[i]) is called by function(item) of this code:

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

correct me if im wrong.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.