-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapImplementationOnPrototype.js
44 lines (31 loc) · 1.39 KB
/
mapImplementationOnPrototype.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// *** 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
// this refers to the caller of the function
// Using a for loop
// for(var i =0; i < this.length; i++){
// newArray.push(callback(this[i]));
// }
this.forEach(num => {
newArray.push(callback(num));
})
// Add your code above this line
return newArray;
};
var new_s = s.myMap(function(item){
return item * 2;
});