-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMap function.js
More file actions
27 lines (18 loc) · 1.09 KB
/
myMap function.js
File metadata and controls
27 lines (18 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*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().*/
var s = [23, 65, 98, 5];
Array.prototype.myMap = function(callback){
var newArray = [];
for(let i = 0; i < this.length; i++){
newArray.push(callback(this[i]));
}
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});
console.log(new_s);