-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_methods.js
More file actions
73 lines (53 loc) · 1.9 KB
/
array_methods.js
File metadata and controls
73 lines (53 loc) · 1.9 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
// 1. Write a function named push that accepts two arguments: an Array and any other value.
// The function should append the second argument to the end of the Array, and return the new length of the Array.
// function push(array, value) {
// array[array.length] = value;
// return array.length;
// }
// let myArray = [1, 2, 3, 4, 5];
// console.log(push(myArray, 6));
// console.log(myArray);
// 2. Write a function named pop that accepts one argument: an Array.
// The function should remove the last element from the array and return it.
// const pop = arr => {
// if (arr.length === 0) {
// return undefined;
// }
// let lastElement = arr[arr.length - 1];
// arr.length = (arr.length - 1);
// return lastElement;
// }
// let myArray = [1, 2, 3, 4, 5];
// console.log(pop(myArray));
// console.log(myArray);
// console.log(pop([]));
// 3. Write a function named unshift that accepts two arguments: an Array and a value.
// The function should insert the value at the beginning of the Array, and return the new length of the array.
// You will need a for loop for this problem.
// const unshift = (arr, value) => {
// for (index = arr.length; index > 0; index -= 1) {
// arr[index] = arr[index - 1];
// }
// arr[0] = value;
// return arr.length;
// }
// let myArray = [1, 2, 3, 4, 5];
// console.log(unshift(myArray, 0));
// console.log(myArray);
// 4. Write a function named shift that accepts one argument: an Array.
// The function should remove the first value from the beginning of the Array and return it.
const shift = arr => {
let firstElement = arr[0];
if (arr.length === 0) {
return undefined;
}
for (index = 0; index < arr.length; index += 1) {
arr[index] = arr[index + 1];
}
arr.length = (arr.length - 1);
return firstElement;
}
let myArray = [1, 2, 3, 4, 5];
console.log(shift(myArray));
console.log(myArray);
console.log(shift([]));