-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevery.html
More file actions
162 lines (147 loc) · 5.38 KB
/
every.html
File metadata and controls
162 lines (147 loc) · 5.38 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<script src="../simpleTest.js"></script>
<script>
// MDN Definition:
// The findIndex() method returns the index of the first element in the array that satisfies the
// provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
// every REQUIREMENTS:
// DONE If all elements in the array are true it should run callback array.length times.
// DONE It should pass the ith element in as the first argument to the callback.
// DONE It should pass the ith position in as the second argument to the callback.
// DONE It should pass the originalArray in as the third agrument to the callback.
// DONE It should accept an optional configurable this object.
// DONE If an element in the array returns false, every should return false instantly.
// DONE If all elements meet the requirement, it should return true.
// DONE It should return true if the array is empty.
// DONE Callback should not run on unassigned indexes.
// DONE Callback should not run on indexes which have been deleted.
// DONE It should not mutate the originalArray.
// DONE It should not run callback on elements which have been appended to the end of the array after the call begins.
// DONE It should not run callback on elements which have been appended to the beginning of the array after the call begins.
// DONE If existing elements are changed, their value should passed at the time that callback visits them.
function every(array, callback, optionalThis) {
var boundLength = array.length;
var everyCallback = callback;
var result = true;
if (optionalThis) {
everyCallback = callback.bind(optionalThis);
}
for(var i = 0; i < boundLength; i++) {
if (i in array) {
var resultCallback = everyCallback(array[i], i, array)
if (resultCallback === false) {
result = false;
return result;
}
}
}
return result;
};
tests({
'If all elements meet the callback requirement, It should run the function array.length times.': function() {
var numberOfTimesCallbackHasRun = 0;
every([1, 2], function() {
numberOfTimesCallbackHasRun++;
});
eq(numberOfTimesCallbackHasRun, 2);
},
'It should pass the ith element in as the first argument to the callback.': function() {
every([1], function(number) {
eq(number, 1);
})
},
'It should pass the ith position in as the second argument to the callback.': function() {
every([1], function(number, index) {
eq(index, 0);
})
},
'It should pass the originalArray in as the third agrument to the callback.': function() {
var myArray = [1];
every(myArray, function(number, index, originalArray) {
eq(myArray, originalArray);
})
},
'It should accept an optional configurable this object.': function() {
every([1], function() {
eq(this.description, 'I am a configurable this');
}, {description: 'I am a configurable this'});
},
'If an element in the array returns false, it should return false.': function() {
var numberCallback = 0;
var everyResult = every([1, 2, 3], function(number) {
numberCallback++;
return number < 2;
})
eq(numberCallback, 2);
eq(everyResult, false);
},
'If all elements meet the requirement, it should return true.': function() {
var everyResult = every([1, 2], function(number) {
return number < 3;
})
eq(everyResult, true);
},
'It should return true if the array is empty.': function() {
var everyResult = every([], function() {})
eq(everyResult, true);
},
'It should exclude holes.': function() {
var numberOfTimesCallbackHasRun = 0;
var myArray = [, 1, , 2, ];
every(myArray, function() {
numberOfTimesCallbackHasRun++;
});
eq(numberOfTimesCallbackHasRun, 2);
},
'It should not run callback on elements that have been deleted.': function() {
var myArray = [1, 2, 3, 1];
var myResult = every(myArray, function(number, index) {
if (index === 0) {
myArray.splice(2, 1);
};
return number < 3;
})
eq(myResult, true);
},
'It should not mutate the originalArray': function() {
var originalArray = [1];
every(originalArray, function() {})
eq(originalArray[0], 1);
eq(originalArray.length, 1);
},
'It should not run callback on elements which have been appended to the end of the array after the call begins.': function() {
var numberOfCallbacks = 0;
var myArray = [1];
var result = every(myArray, function(value) {
myArray.push(2);
numberOfCallbacks++
return value < 2;
});
eq(numberOfCallbacks, 1);
eq(result, true);
},
'It should not run callback on elements which have been appended to the beginning of the array after the call begins.': function() {
var numberOfCallbacks = 0;
var myArray = [1];
var result = every(myArray, function(value) {
myArray.splice(0, 0, 2);
numberOfCallbacks++
return value < 2;
});
eq(numberOfCallbacks, 1);
eq(result, true);
},
'If existing and unvisited elements are changed by callback, it should use the value at the time every visits the elements index.': function() {
var numberOfTimesCallbackHasRun = 0;
var myArray = [1, 2, 3]
var result = every(myArray, function(number, index) {
if (index === 0) {
myArray[1] = 4;
}
numberOfTimesCallbackHasRun++;
return number < 4;
})
eq(myArray[1], 4);
eq(result, false);
}
});
</script>