Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save afaqahmedkhan/1d66a063725506b59419906acd8f7654 to your computer and use it in GitHub Desktop.
Save afaqahmedkhan/1d66a063725506b59419906acd8f7654 to your computer and use it in GitHub Desktop.
Functional Programming: Implement map on a Prototype
Functional Programming: Implement map on a Prototype
As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't.
In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.
It would teach us a lot about map to try to implement a version of it that behaves exactly like the Array.prototype.map() with a for loop or Array.prototype.forEach().
Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.
Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You may use a for loop or the forEach method.
// 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;
});
@HusnainButt12
Copy link

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], i , this))}

// Only change code above this line
return newArray;

};

@PDGONZALE1
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment