Implement map on a Prototype: my issue

Implement map on a Prototype: my issue
0

#1

Tell us what’s happening:
So a few things i am confused about. First what is callback and what is its purpose? How am i to use it in this function? I did not know what it was so i did not use it and figured my code below should work. I copy the s array into the newArray and then perform operations on the elements of newArray and return but for some reason newArray is not being changed. Its still the same 23, 65, 98, 5 array. Any idea why?
P.S I eventually had to create love just so i could pass the challenge. But i still want to now why return newArray originally did not work

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
  newArray = [...s];
  //console.log(newArray);
  var love = [];
  for(let i = 0; i < newArray.length; i++){
      console.log(newArray[i]);
      love.push(newArray[i] * 2);
  }
  // Add your code above this line
  console.log(love);
  return love;

};

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

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


#2

The callback parameter of the anonymous function allows you to execute the function passed to the myMap method. I should be able to create any function which takes a single argument and returns a new value by modifying the argument in some way. I then should be able to pass this function to your myMap method, which would be called on an array. Your myMap method would return a new array with values which have been created via the function I pass to your myMap method.

In the challenge, the function passed to your myMap method as the callback argument is as follows:

function(item){
  return item * 2;
}

The function returns the value of item multiplied by 2. If you myMap method is working correctly, it will use call this function on every element of the array on which the myMap method is called and generate a new array which contains the original array’s values multiplied by 2.

Your for loop with the love array basically hardcoded the function, but your function should be able to take any function passed and make the calculations.


#3

I do not know what code you tried for above, because your posted solution used the hardcoded function with the love array. If you want to reply back with what you tried originally which returned the same array, I can respond back with why it does not work.


#4

so why did i have to do another hardcode of multiplying the array elements by 2? I cant fifgure out how to use callback in the myMap function.


#5

You did not have to, you just did.

callback is the function that is passed into the myMap method. How do you normally execute a function? Since callback is a function which accepts one argument and I have a value (let’s say 10), I can write:

callback(10);

If the value is a variable named val, I can write:

callback(val);

Then you could push the returned value of the callback execution to an array.

The “trick” to this challenge is figuring out how to reference the array in which the myMap method is called upon. You are currently using newArray = […s], because s has been globally declared, but I should be able to call your mapMap method on a literal array with a different function like:

var mappedArr = [1, 2, 3, 4, 5].myMap(function(item) {
  return item * 3;
});

If implemented correctly, mappedArr would look like [3, 6, 9, 12, 15]