-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.html
More file actions
201 lines (177 loc) · 6.91 KB
/
slice.html
File metadata and controls
201 lines (177 loc) · 6.91 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<script src="../simpleTest.js"></script>
<script>
// MDN Definition:
// The slice() method returns a shallow copy of a portion of an array into a
// new array object selected from start to end (end not included) where start
// and end represent the index of items in that array. The original array will
// not be modified.
// NOTES:
// slice REQUIREMENTS:
// DONE It should return an array.
// DONE It should return a new array.
// DONE It should return a new array containing the selected elements of the array.
// DONE If an optional second argument is passed in, it should start copying from the startingIndex spot established by the second argument value.
// DONE If an optional third argument is passed in, it should stop copying when it reaches the (lastIndex) index location established by the value of the third argument.
// DONE If no lastIndex, it should copy to the end of the array.
// DONE If startingIndex is a negative number, and no lastIndex, it should offset the beginning of the search that many indexes from the end of the array.
// DONE If startingIndex is a negative number, and lastIndex is included, it should offset the beginning of the search that many indexes from the end of the array too.
// DONE If startingIndex is greater than array.length, it should return an empty array.
// DONE If lastIndex is a negative number, it should offset the end of the search that many indexes from the end of the array.
// DONE If lastIndex is greater than array.length, it should copy through the end of the array.
// DONE It should return a shallow copy of objects.
// If a change is made to a referenced object, change should be visible in both
// originalArray and newArray.
// DONE If a string is copied, it should copy the value into the new array.
// DONE If a number is copied, it should copy the value into the new array.
// DONE If a boolean is copied, it should copy the value into the new array.
// Changes to the originalArray should not affect these.
// DONE It should not alter the originalArray.
// DONE If an element is added to originalArray, newArray should not be affected.
// DONE If an element is added to newArray, originalArray should not be affected.
function slice(array, startingIndex, lastIndex) {
var boundLength = array.length - 1;
var newArray = [];
var fixedStartingIndex = 0;
var fixedLastIndex = boundLength;
// Setting starting Index
if (startingIndex) {
if (startingIndex > array.length) {
return newArray;
} else if (startingIndex < 0) {
fixedStartingIndex = array.length + startingIndex;
} else {
fixedStartingIndex = startingIndex;
}
}
// Setting last Index
if (lastIndex) {
if (lastIndex < 0) {
fixedLastIndex = array.length + lastIndex;
} else if (lastIndex > array.length) {
fixedLastIndex = array.length;
} else {
fixedLastIndex = lastIndex;
}
}
// Creating New Array
if (arguments.length === 3) {
for (var i = fixedStartingIndex; i < fixedLastIndex; i++) {
newArray.push(array[i]);
}
return newArray;
} else if (arguments.length === 2) {
for (var j = fixedStartingIndex; j < array.length; j++) {
newArray.push(array[j]);
}
return newArray;
} else {
for (var k = 0; k < array.length; k++) {
newArray.push(array[k]);
}
return newArray;
}
return newArray;
}
tests({
'It should return an array.': function() {
var result = slice([1]);
eq(Array.isArray(result), true);
},
'It should return a new array.': function() {
var originalArray = [1];
var result = slice(originalArray);
eq(result !== originalArray, true);
},
'It should return a new array containing the selected elements of the array.': function() {
var myArray = [1];
var result = slice(myArray);
eq(result, 1);
},
'If an optional second argument is passed in, it should start copying from the startingIndex spot established by the second argument value.': function() {
var myArray = [1, 2];
var result = slice(myArray, 1);
eq(result, 2);
},
'If an optional third argument is passed in, it should stop copying when it reaches the (lastIndex) index location established by the value of the third argument.': function() {
var myArray = [1, 2, 3];
var result = slice(myArray, 1, 2);
eq(result, 2);
},
'If no lastIndex, it should copy to the end of the array.': function() {
var myArray = [1, 2, 3];
var result = slice(myArray, 1);
eq(result, ('2,3'));
},
'If startingIndex is a negative number, and no lastIndex, it should offset the beginning of the search that many indexes from the end of the array.': function() {
var myArray = [1, 2, 3];
var result = slice(myArray, -1);
eq(result, 3);
},
'If startingIndex is a negative number, and lastIndex is included, it should offset the beginning of the search that many indexes from the end of the array too.': function() {
var myArray = [1, 2, 3];
var result = slice(myArray, -1, 3);
eq(result, 3);
},
'If startingIndex is greater than array.length, it should return an empty array.': function() {
var myArray = [1];
var result = slice(myArray, 2);
eq(result.length, 0);
},
'If lastIndex is a negative number, it should offset the end of the search that many indexes from the end of the array.': function() {
var myArray = [1, 2, 3];
var result = slice(myArray, 0, -1);
eq(result.length, 2);
},
'If lastIndex is greater than array.length, it should copy through the end of the array.': function() {
var myArray = [1, 2];
var result = slice(myArray, 0, 3);
eq(result.length, 2);
},
'It should make a shallow copy of objects in the originalArray.': function() {
var myArray = [[1]];
var result = slice(myArray);
myArray[0].push(3);
eq(result[0].length, 2);
},
'If a string is copied, it should copy the value into the new array.': function() {
var myArray = ['old'];
var result = slice(myArray);
myArray = ['new'];
eq(result, 'old');
eq(myArray, 'new');
},
'If a number is copied, it should copy the value into the new array.': function() {
var myArray = [2];
var result = slice(myArray);
myArray = [3];
eq(result, 2);
eq(myArray, 3);
},
'If a boolean is copied, it should copy the value into the new array.': function() {
var myArray = [true];
var result = slice(myArray);
myArray = false;
eq(result, 'true');
eq(myArray, false);
},
'It should not alter the original array.': function() {
var myArray = [1];
var result = slice(myArray);
eq(myArray, 1);
},
'If an element is added to originalArray, newArray should not be affected.': function() {
var myArray = [1];
var result = slice(myArray);
myArray.push(3);
eq(myArray.length, 2);
eq(result.length, 1);
},
'If an element is added to newArray, originalArray should not be affected.': function() {
var myArray = [1];
var result = slice(myArray);
result.push(3);
eq(myArray.length, 1);
eq(result.length, 2);
}
});
</script>