Implement map on a Prototype question about solution 1

Tell us what’s happening:
Describe your issue in detail here.

Hello, I have few questions about this challenge(I’m new to coding so they may sound silly or irrelevant):

1- why we used i < this.length and not i < callback.length? isn’t (callback) the parameter of the function which we will insert our array into and what does this refer to here?

2- is Array a constructor? if yes then at the end of the challenge const new_s = s.myMap(function(item){return item *2;}) are we treating s as an instance of Array and not a const as it was declared at the start?

3- when I try to multiply an array with a number it gives me NaN, how are we multiplying by 2 in this challenge?

this is the answer:

// 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 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 < callback.length; i++){
newArray.push(callback[i]);
};
// Only change code above this line
return newArray;
};

const 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/95.0.4638.69 Safari/537.36 OPR/81.0.4196.61

Challenge: Implement map on a Prototype

Link to the challenge:

1- why we used i < this.length and not i < callback.length ? isn’t (callback) the parameter of the function which we will insert our array into and what does this refer to here?

What does i < callback.length mean? callback is a function, they don’t have lengths. In this context here, this refers to the array itself - that is the length we want.

is Array a constructor?

Yes.

are we treating s as an instance of Array and not a const as it was declared at the start?

That doesn’t make sense. Those are not mutually exclusive. “const” just refers to whether or not the variable can be reassigned. It doesn’t matter if it is an instance of Array or not.

3- when I try to multiply an array with a number it gives me NaN

Because you can’t. That makes no sense.

how are we multiplying by 2 in this challenge?

Because we are multiplying a number, not an array.

You are passing in your callback function:

function(item) {
  return item * 2;
}

It is applied here:

for (let i = 0; i < this.length; i++) {
  newArray.push(callback(this[i]));
}

So this is what you are sending to your callback:

this[i]

In this case, this is an array, so this[i] is an element of that array. Since the array is [23, 65, 98, 5], your callback is going to be passed each of those elements; it will be passed 4 numbers, one at a time.

1 Like

Thank you for your detailed reply it clarified most of my questions.

when i hover my pointer on callback it says parameter as shown in the attached picture bellow. is it a typo/bug?

It is a parameter. It is a parameter being passed into the function. A parameter can be any JS type, including a function. (In JS, functions are variables, a specialized object. Remember that in JS, all reference types are types of objects.)

I assume that the “any” there means the type. In some languages you can specify the type of a parameter or variable (But remember that in JS, types are associated with data, not variables.) In some languages you must declare the type of all variables. Standard JS does not.

1 Like

Thank you for your detailed answer, much appreciated,