What is `callback(this[i])?`

Tell us what’s happening:
i don’t know understand **callback(this[i])** in this solution, pls help me !!!
what is callback(this[i]) for ?

// 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.push(callback(this[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/79.0.3945.88 Safari/537.36.

Challenge: Implement map on a Prototype

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

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

callback is, well, a callback :stuck_out_tongue:
It’s a function you pass to myMap method; it will then be called once for each cycle of the loop you have written^^
this[i] refers to the caller (the array you will call myMap method against) at i index^^

1 Like

I will do it next time, thanks :heart_eyes:

Thks you so much.Now i have figured it out :heart_eyes:

1 Like

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