-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyFindIndex.html
More file actions
132 lines (121 loc) · 4.39 KB
/
myFindIndex.html
File metadata and controls
132 lines (121 loc) · 4.39 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<script src="..\my-jsArrayMethods\simpletest.js"></script>
<script>
/*
The findIndex() method returns the index of the first element in the array...
that satisfies the provided testing function.
Otherwise, it returns -1...
indicating no element passed the test.
Parameters
callback
A function to execute on each value in the array...
until the function returns true...
indicating the desired element was found.
It takes 3 arguments:
element
The current element being processed in the array.
indexOptional
The index of the current element being processed in the array.
arrayOptional
The array findIndex was called upon.
thisArgOptional
Optional. Object to use as this when executing callback.
Return Value:
an index of the first array element that passes callback test;
otherwise, -1.
*/
// the same as find() except two specific requirements:
// findIndex returns the found index instead of the value
// findIndex returns -1 if the index is not in the array.
Array.prototype.myFindIndex = function (callback, optionalThis) {
if (typeof callback !== 'function') {
throw new TypeError('callback is not a function');
}
if (optionalThis) {
callback = callback.bind(optionalThis);
}
var input = this;
for (var i = 0; i < input.length; i++) {
if (callback(input[i], i, this)) {
return i;
}
}
return -1;
}
tests({
'It should throw type error if callback is not a function.': function() {
var isTypeError = false;
try{
[1, 2, 3].myFindIndex('not a function');
} catch(e) {
isTypeError = (e instanceof TypeError);
}
},
'It should not mutate the array on which it is called.': function() {
var myArray = [1, 2, 3, 4];
myArray.myFindIndex(function(element) {
return ((element % 2 === 0));
});
eq(myArray[0], 1);
eq(myArray[1], 2);
eq(myArray[2], 3);
eq(myArray[3], 4);
},
'It should return -1 if no element is found that passes callback test.': function() {
var myArray = [1, 3, 5, 7];
eq(myArray.myFindIndex(function(element) {
return ((element % 2 === 0));
}), -1);
},
'It should run callback on all array indexes, including holes.': function() {
var myArray = [, 3, , 7];
var callbackRunCount = 0;
myArray.myFindIndex(function(element, index, array, optionalThis) {
callbackRunCount ++;
});
eq(callbackRunCount, 4);
},
'Callback should have access to each element in the array.': function() {
var myArray = ['alex', 'music', 'san francisco'];
var testArray = [];
myArray.myFindIndex(function(element, index, array, optionalThis) {
testArray.push(element);
});
eq(testArray[0], 'alex');
eq(testArray[1], 'music');
eq(testArray[2], 'san francisco');
},
'Callback should have access to the index of each array element.': function() {
var myArray = ['alex', 'music', 'san francisco'];
var testArray = [];
myArray.myFindIndex(function(element, index) {
testArray.push(index);
});
eq(testArray[0], 0);
eq(testArray.length, 3);
},
'Callback should have access to the array on which it is called.': function() {
var myArray = ['alex', 'music', 'san francisco'];
myArray.myFindIndex(function(element, index, array) {
eq(array, myArray);
});
},
'It should bind callback to optionalThis argument if provided.': function() {
var myArray = [1, 3, 5, 7];
myArray.myFindIndex(function(element, index, array) {
eq(this.name, 'I am bound');
}, {name: 'I am bound'});
},
'It should return the first index of the array that passes callback test.': function() {
var myArray = [1, 2, 3, 4];
eq(myArray.myFindIndex(function(element) {
return ((element % 2 === 0));
}), 1);
}
// 'It should...': function() {},
// 'It should...': function() {},
// 'It should...': function() {},
// 'It should...': function() {},
// 'It should...': function() {},
// 'It should...': function() {}
})
</script>