A Pen by Afaq Ahmed Khan on CodePen.
Created
November 24, 2018 06:19
-
-
Save afaqahmedkhan/1d66a063725506b59419906acd8f7654 to your computer and use it in GitHub Desktop.
Functional Programming: Implement map on a Prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); |
I would like to know if this line of reasoning would be correct for solving the exercise
Yeah @gabrielmanoel-hub , among all the provided solution, your solution would definetely work for all the test cases.
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;
};
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
var array1 = [23, 65, 98, 5, 13]
var array2 = ["naomi", "quincy", "camperbot"]
var array3 = [1, 1, 2, 5, 2]
Array.prototype.myMap = function(callback){
var newArray = [];
// Add your code below this line
for(let i = 0; i < this.length ; i++ ){
// callback(this[i], i)
newArray.push(callback(this[i], i, this));
}
// Add your code above this line
return newArray;
};
var new_s = array1.myMap(function(item){
return item * 2;
});
var new_s2 = array2.myMap(function(item){
return item.toUpperCase();
});
var new_s3 = array3.myMap(function(itens, index, array){
return array[index + 1] || array[0];
});