-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevery.html
More file actions
111 lines (97 loc) · 2.92 KB
/
every.html
File metadata and controls
111 lines (97 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
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
<script src="../simpletest.js"></script>
<script>
// Prototype Function
// function every(originalArray, callback) {
// for(var i = 0; i < originalArray.length; i++) {
// if(!callback(originalArray[i], i, originalArray)) {
// return false;
// }
// }
// return true;
// }
function every(originalArray, callback, optionalThisArgument) {
var functionCallback = callback;
var maxRunCount = originalArray.length;
if(Object.keys(originalArray).length === 0) {
return true;
}
if (arguments.length >= 3) {
functionCallback = callback.bind(optionalThisArgument);
}
for(var i = 0; i < maxRunCount; i++){
if(i in originalArray && !functionCallback(originalArray[i], i, originalArray)) {
return false;
}
}
return true;
}
tests({
'It should immediately return false if the callback returns false for an element': function () {
var runCount = 0;
var functionResult = every([1,2,3], function(element){
runCount++;
return element < 2;
})
eq(functionResult,false);
eq(runCount,2);
},
'It should return true or false.' : function (){
var functionResult = every([1,2,3], function(element){
return element < 4;
})
eq(functionResult,true);
},
'It should pass in the ith element as the first argument to the callback': function() {
every([3],function(number){
eq(3, number);
})
},
'It should pass in the ith position as the second arguement to the callback': function() {
every([1],function(number, index){
eq(0,index);
})
},
'It should pass in the original array as the third argument to the callback': function() {
var testArray = [1,2,3];
every(testArray, function (number, index, originalArray){
eq(originalArray,testArray);
})
},
'It should accept an optional this object': function () {
every([1], function() {
eq(this.description,"I should be accessible within the callback");
}, {description:"I should be accessible within the callback"})
},
'It should not modify the original array': function () {
var testArray = [1,2,3];
var testCopy = [1,2,3];
every(testArray,function (){});
for(var i = 0; i < testArray.length; i++) {
eq(testArray[i],testCopy[i]);
}
},
'It should return true for an empty array': function() {
var functionResult = every([],function(){
return false;
});
eq(functionResult,true);
},
'It should run <= originalArray.length times.': function () {
var runCount = 0;
every([1,2,3],function(number, index, array){
runCount++;
array.push('66');
return number > 0;
})
eq(runCount,3);
},
'It should only run for elements in the array.': function() {
var runCount = 0;
every([,2,3,4,],function(number, index, array){
runCount++;
return number > 0;
})
eq(runCount,3);
}
});
</script>