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