-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincludes.html
More file actions
69 lines (57 loc) · 1.93 KB
/
includes.html
File metadata and controls
69 lines (57 loc) · 1.93 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
<script src="../simpletest.js"></script>
<script>
/*
Prototype Function
function includes(originalArray, searchValue, optionalStartingPostion) {
var startingPosition = 0;
if(arguments = 3) startingPosition = optionalStartingPostion;
for(var i = startingPosition; i < originalArray.length; i++) {
if(originalArray[i] === searchValue)) {
return true;
}
}
return false;
}
*/
function includes(originalArray, searchValue, optionalStartingPostion) {
var startingPosition = 0;
if(arguments.length === 3) {
if(optionalStartingPostion < 0) {
optionalStartingPostion = originalArray.length + optionalStartingPostion;
}
startingPosition = optionalStartingPostion;
}
for(var i = startingPosition; i < originalArray.length; i++) {
if(originalArray[i] === searchValue) {
return true;
}
}
return false;
};
tests({
'It should return a boolean': function() {
var result = includes([1,2,3],3);
eq(typeof result === 'boolean',true);
},
'It should return true if any element in the array matches the search value': function() {
var result = includes([1,2,3],2);
eq(result,true);
},
'It should return false if no element in array matches the search value': function() {
var result = includes([1,2,3],4);
eq(result,false);
},
'It should accept an optional starting position as third argument': function () {
var result = includes([1,2,3,4],1,1);
eq(result,false);
},
'If starting position is greater than the size of the array, false is returned': function () {
var result = includes([1,2,3],1,3);
eq(result,false);
},
'If starting postion is negative, it is treated as an offset from the end of the array, e.g. -1 = last element in the array' : function () {
var result = includes(['corn','wheat','rice'],'wheat',-1);
eq(result,false);
}
});
</script>