Implement map on a Prototype..... identifier expected problem

Implement map on a Prototype..... identifier expected problem
0

#1

At the line function(item) { for… } i get an identifier expected… dont know what it means…

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
  function (item) {
    for(var i = 0; i < item.length; i++) {
      newArray.push(callback(item[i]));
    }
  }
  // Add your code above this line
  return newArray;

};

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

Your browser information:

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

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


#2

You don’t need to create another function inside, because that’s what a callback parameter will be when given as an argument.
The only thing you need to do is to process every element of the provided array with that callback function when adding to the array you need to return.

Using a loop, that’s two -three lines of code. Using forEach that’s just a one-liner :slight_smile:
Also, you may read about this when working with arrays to give you a hint :slight_smile:


#3

Also in this line why do u do callback(this[i]) or callback(item[i]) instead of just directly typing newArray.push(item[i])… since it is already inside the function callback


#4

Again, you don’t need a function with a parameter item. I guess you may use it, but it is unnecessary. It’s just newArray.push(callback(i)) then, if you had to write for (let i = 0; i < this.length; i++). This refers to the array being manipulated and because you call an Array.prototype's function on any kind of array, you iterate though it’s items automatically with a loop or a forEach() function.
Now, to answer your question: if you pushed i just like that, you would essentially return the same array, because you wouldn’t have passed i to be processed by a callback function.


#5

Hmm i think i get the gist of things…
so since Array.prototype.myMap = function(callback) {… }

when we assign new_s = s.myMap(function(item) {....} does it become something like function(callback(item)) {....} where function(item) will be equivalent to callback function and above it, it will be having a looping function using for or forEach… is that what happens? am i close enough? :thinking:


#6

I’m not sure I understood you well (English is my 2nd lang after all) but here’s the bottom line:
We have an array of elements but we want to get a new array with those elements’ values changed/mapped, so we write a function to do this job. In your code that would be the inner function function (item) {} Here’s how a standalone version of your function should look like:

function myMap(item, callback) {
  let newArray = [];
  for (let i = 0; i < item.length; i++) {
    newArray.push(callback(item[i]));
  }
  return newArray;
}

Now you can invoke this function like that:
myMap([1, 2, 3, 4], (x) => x * 2)

But you only need the function body, because you’re building a new method to the Array.prototype!

When you assign this function to the method name myMap, you remove the item parameter because you can now refer to the array you’re working with, with this because the method belongs to the Array.prototype.

Examine this: this.forEach(x => newArray.push(callback(x)) );
And here’s how to translate the line myArray.myMap(x => x * 2):
for each element x in myArray, push x processed by a function (x) => x * 2 to the newArray.

You build whatever function you want for a callback argument in your function call and the method you’ve just built makes sure to return an array of elements, which have been processed by that callback function.
I apologize if it’s to verbose, I rarely go into such lengths of explanations. I just hope it helps


#7

its ok… thanks for the help… i guess the more i practise callbacks the less confusing it will be getting…

so to summarize the callback function is the x => x*2; part that every value of the array passes through


#8

i was trying to drawing a comparison between the callback function being the same as function(item)

but i guess its not as simple as that.


#9

The answer is a simply no, they’re not the same. A callback function is just another function, passed as an argument to another function. You either pre-define it, or define in the argument list, so your function body, needed for myMap is separate from a callback function. In the former you just apply some steps to iterate through the array being worked with to return a new array and in the latter you define what do you actually want to do with those elements and call it when pushing the result to that new array.