-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy patharrays.js
45 lines (36 loc) · 1.27 KB
/
arrays.js
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
var chocolateBars = ["snickers", "hundred grand", "kitkat", "skittles"];
var addedChocolateBar = "foo";
function addElementToBeginningOfArray(chocolateBars, addedChocolateBar) {
var moreChocolateBars = [addedChocolateBar,...chocolateBars]
return moreChocolateBars;
}
function destructivelyAddElementToBeginningOfArray(chocolateBars, addedChocolateBar) {
chocolateBars.unshift(addedChocolateBar);
return chocolateBars;
}
function addElementToEndOfArray(chocolateBars, addedChocolateBar) {
var moreChocolateBars = [...chocolateBars,addedChocolateBar]
return moreChocolateBars;
}
function destructivelyAddElementToEndOfArray(chocolateBars, addedChocolateBar) {
chocolateBars.push(addedChocolateBar);
return chocolateBars;
}
function accessElementInArray(array, index) {
return array[index];
}
function destructivelyRemoveElementFromBeginningOfArray(chocolateBars) {
chocolateBars.shift();
return chocolateBars;
}
function removeElementFromBeginningOfArray (chocolateBars) {
return chocolateBars.slice(1);
}
function destructivelyRemoveElementFromEndOfArray(chocolateBars) {
chocolateBars.pop();
return chocolateBars;
}
function removeElementFromEndOfArray(chocolateBars) {
chocolateBars = chocolateBars.slice(0, chocolateBars.length - 1);
return chocolateBars;
}