-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyMap.html
More file actions
79 lines (76 loc) · 2.92 KB
/
myMap.html
File metadata and controls
79 lines (76 loc) · 2.92 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<script src="..\my-jsArrayMethods\simpletest.js"></script>
<script>
// My version of the native Array.prototype.map function.
// Parameters
// array
// callback function which takes three arguments.
// currentValue: The current element being processed in the array.
// indexOptional: The index of the current element being processed in the array.
// arrayOptional: The array map was called upon.
// optionalThis
// Return Value
// A new array containing the results of callback function on each element.
function myMap(array, callback, optionalThis) {
// var boundCallback = callback; /* unnecessary variable declaration */
if (optionalThis) {
// boundCallback = callback.bind(optionalThis);
callback = callback.bind(optionalThis);
}
var mappedArray = [];
for (var i = 0; i < array.length; i ++) {
if (i in array) {
mappedArray[i] = callback(array[i], i, array);
}
}
return mappedArray;
};
tests({
'It should run callback function array.length times.': function() {
var callbackCount = 0;
myMap([1, 2, 3], function() {
callbackCount ++;
});
eq(callbackCount, 3);
},
'It should pass the current element being processed as first argument to callback.': function() {
myMap([1], function(el) {
eq(1, el);
});
},
'It should pass callback the current element index as second argument.': function() {
myMap([1], function(el, index) {
eq(0, index);
});
},
'It should pass callback the originalArray as third argument': function() {
var testArray = [1, 2, 3];
myMap(testArray, function(el, index, originalArray) {
eq(testArray, originalArray);
})
},
'It should accept an optional this argument to be used by callback.': function() {
myMap([1, 2, 3], function() {
eq(this.description, 'I am optionalThis arg in callback.');
}, {description: 'I am optionalThis arg in callback.'});
},
'It should not modify originalArray.': function() {
var originalArray = [];
var mappedArray = myMap(originalArray, function() {});
eq(originalArray === mappedArray, false);
},
'It should return new array that is the same length as originalArray.': function() {
var myMapTest = myMap([1, 2, 3], function(el) {
return el + 1;
});
eq(myMapTest.length, 3);
},
'It should return new array containing results of callback on each el of originalArray.': function() {
var myMapTest = myMap([1, 2, 3], function(el) {
return el + 1;
});
eq(myMapTest[0], 2);
eq(myMapTest[1], 3); // Added additional els to test callback on multiple elements of the array.
eq(myMapTest[2], 4);
},
});
</script>