-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray Mappings
More file actions
28 lines (19 loc) · 732 Bytes
/
Array Mappings
File metadata and controls
28 lines (19 loc) · 732 Bytes
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
/*
Array Mappings
Oh no, the map method for arrays has been disabled. Can you fix it?
In case you haven't come across the map method for arrays, here is how it works:
[1,2,3].map(x => x ** 2) === [1,4,9]
[1,2,3].map(x => 2 * x) === [2,4,6]
[1,2,3].map(x => 2 ** x) === [2,4,8]
[1,2,3].map(x => x.toString()) === ["1","2","3"]
["1","2","3"].map(x => parseInt(x)) === [1,2,3]
["random","array","to","be","mapped"].map(x => mapping(x)) === [mapping("random"),mapping("array"),mapping("to"),mapping("be"),mapping("mapped")]
The map method does not mutate the original array.
*/
Array.prototype.map = function (el){
let newArr = [];
for(let i = 0; i < this.length; i++){
newArr.push(el(this[i],i))
}
return newArr
}