-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.html
More file actions
115 lines (100 loc) · 3.23 KB
/
filter.html
File metadata and controls
115 lines (100 loc) · 3.23 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
<script src="../tinytest.js"></script>
<script>
// Example 1:
// filter([1, 2, 3], function() {});
//Example 2:
// filter([1, 2, 3], function(number) {
// console.log(number);
// });
// Example 3:
// filter([1, 2, 3], function(number, index) {
// console.log(index);
// });
// Example 4:
// filter([1, 2, 3], function(number, index, originalArray) {
// console.log(originalArray);
// });
// Example 5:
// filter([1, 2, 3], function() {
// console.log(this.name);
// }, {name: 'Gordon'});
// Example 6:
// var testArray = [1, 2, 3];
// var filteredArray = filter(testArray, function() {});
// testArray !== filteredArray
// Example 7:
// var filteredArray = filter(testArray, function(element) {
// return element > 1;
// });
// Expect filteredArray to be [2, 3]
function filter(originalArray, callback, optionalThisObject) {
var filterCallback = callback;
if (optionalThisObject) {
filterCallback = callback.bind(optionalThisObject);
}
var filteredArray = [];
for (var i = 0; i < originalArray.length; i++) {
if (filterCallback(originalArray[i], i, originalArray)) {
filteredArray.push(originalArray[i]);
}
}
return filteredArray;
}
tests({
'If no elements meet callback requirements, It should run the callback function array.length times.': function() {
var numberOfTimesCallbackHasRun = 0;
filter([1, 2, 3], function() {
numberOfTimesCallbackHasRun++;
});
eq(numberOfTimesCallbackHasRun, 3);
},
'It should run the callback function only until a match is found.': function() {
var numberOfTimesCallbackHasRun = 0;
filter([1, 2, 3], function(number) {
return number > 2;
numberOfTimesCallbackHasRun++;
});
eq(numberOfTimesCallbackHasRun, 2);
},
'It should pass in the ith element as the first argument to the callback.': function() {
filter([1], function(number) {
eq(number, 1);
});
},
'It should pass in the ith position as the second argument to the callback.': function() {
filter([1], function(number, index) {
eq(index, 0);
});
},
'It should pass in the original array as the third argument to the callback.': function() {
var testArray = [1, 2, 3];
filter(testArray, function(number, index, originalArray) {
eq(originalArray, testArray)
});
},
'It should accept an optional this object': function() {
filter([1], function() {
eq(this.description, 'I am a configurable this object')
}, {description: 'I am a configurable this object'});
},
'It should return an array.': function() {
var testArray = [];
var filteredArray = filter(testArray, function() {});
//filteredArray is actually an array
eq(Array.isArray(filteredArray), true);
},
'It should return a new array, not the array being filtered.': function() {
var arrayBeingFiltered = [];
var filteredArray = filter(arrayBeingFiltered, function() {});
//filteredArray is not equal to testArray
eq(filteredArray !== arrayBeingFiltered, true);
},
'It should return a new array that only has elements where callback returns true': function() {
var filteredArray = filter([1, 2], function(number) {
return number > 1;
});
eq(filteredArray.length, 1);
eq(filteredArray[0], 2);
}
});
</script>