Hard time to understand the solution!

Tell us what’s happening:

I cannot understand the following:

why we need to .push(callback(a)), since ‘a’ is already the iteration in this, therefore I’m iterating to whatever object is passed in.
Why do I need to pass.push(callback) all together ?

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
  this.forEach(a => newArray.push(callback(a)));
  // 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/78.0.3904.97 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

if you do .push(callback) you are pushing a function to the array
if you do .push(callback(a)) you are pushing the returned value from the function, the output of the function to the array
which one do you want?

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.

Ok I will. Thank you

But why can’t i just .push(a) directly. Isn’t a the current iteration on callback?

a=> this => callback ? isn’t that the logic behind? or I’m missing something?

if you do .push(a) you are just taking the current value of a and putting it in the array.

You need to change the value before pushing it to the array, using the passed in function (callback)

the only way for the function to give an output is to call it

forEach does something for each element of the array, you need to tell what to do though. It will do exactly what you write, nothing less, nothing more.