Tell us what’s happening:
Describe your issue in detail here.
Hi! I really can’t get the idea, how these functions works. So I have 2 questions:
First — do I understand the logic of the programm correctly?`
-
const new_s = s.myMap(function(item)
startsArray.prototype.myMap = function(callback)
-
Array.prototype.myMap = function(callback)
takesconst s
as an array anditem
(new_s) as an argumet (callback) - Than it pushes all elements of array
S
tonewArr
-
Array.prototype.myMap = function(callback) takes
returnnewArr = [23, 65, 98, 5]
. So it creates a copy of S and don’t change S -
const new_s = s.myMap(function(item)
* multipliesnewArr
which is returned asitem
into it and return it
Second — why s.myMap(function(item)
inderstands that item
is new_s
? Why isn’t it S or something else?
**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 < this.length; i++) {
newArray.push(callback(this[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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36
Challenge: Implement map on a Prototype
Link to the challenge: