when do I need to use callback argument, I don’t really understand how to do it, it seems to me logic and correct here is it my solution:
// 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 < s.length; i++){
newArray.push(s[i])
}
// Add your code above this line
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});
What do you think you want to push onto newArray? Currently, you’re using the global array (which isn’t really what you want), and you’re pushing that global array member at the given index position.
Thinking logically, within Array.prototype.myMap(), what do you think this (the context) would be? Try doing the following:
Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
// *this* is a reserved keyword in javascript, representing the current scope. Can we use
// it within our myMap() to get access to the passed Array?
console.log(this); // <-- what does it show you?
// and the rest as you have it...
for (let i = 0; i < /* what do you want to use here? */; i++){
// Now here, do you want to push the value from the array itself? Or do you want to
// somehow apply *callback* to the value from the array, and push that?
newArray.push(/* We don't want to use the global here - do we have a reference to the array that is running myMap? */);
}
// add your code above this line
return newArray;
};
// Now here, do you want to push the value from the array itself? Or do you want to
// somehow apply callback to the value from the array, and push that?
newArray.push(/* We don’t want to use the global here - do we have a reference to the array that is running myMap? */);
I think it’s right if we push the callback to let it behaves as it will be defined later like the arg in the map()method so we need to use callback(s[i]) so s[i] it changes through the array elements so we can say that’s the argument (a) on the map()method (s.map(a => a)).
so a here is a callback function and passed as an argument on map method as s[i] passed on Array.prototype.myMap so the callback is the name of the function
but I still have a question that makes me confused about is a = callback or a = s[i]?
because when I try this on the console it gives me that?
s.myMap(function(a){ return a }) //[23, 65, 98, 5]
Again, you want to avoid using s (the global array). What if someone wants to use your function with another array?
Did you try the console.log(this) like I had told you?
What you want to push onto the newArray is the returned value of executing the callback with the current array[index] member. You don’t have an a. Hell, you don’t even have an s inside your myMap(). How should you refer to the Array on which you’re operating? (hint: what’s this?)
Have you played with repl yet? I have your code saved into one, and we can do a quick “pair programming” on it if you like.
I have created a multi-user session on repl (so more than one user can edit and comment at a time), and I’ll keep it open until 4:30pm UTC-5 (so, like, 25 minutes).
Edit Shutting down the shared repl.
So here’s the thing: first, within the Array.prototype.myMap(), the reserved keyword this refers to the array upon which we’re working. So we could, in theory, do something like:
Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
// Remember, in the following line, `this` refers to the current Array on
// which we're working. So `this.length` is the length of the array we've called the
// function on (with our s.myMap(), it refers to s, but if I did ['a','b','c','fish'].myMap(),
// it would refer to that dynamically created array.
for (let i=0; i<this.length; i++){
newArray.push(/* here we want to push the returned value of the callback, to which
we pass the this[i] as a parameter. */ );
}
// Add your code above this line
return newArray;
yes you’re right, I just not understand the this keyword and its behaviors well, so it refers to the array we are willing to work on
thanks a lot for your effort and your support I really appreciate it, I wasn’t here when you open the repl.it session I’m so sorry, thanks a lot again for your patient and effort
Exactly. Within your function, this refers to the Object from which the function was called. As we had attached our myMap to the Array prototype, then any array that uses our myMap will, within the myMap, be known as this.
So, given that, and knowing you want to push the return value of calling the callback function with this[i] as its parameter (callback (this[i])), what do you think is the next step?
yes, the next step is to push the callback func as a value onto the newArray am’i right?
this my solution:
// 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;
});