Skip to content

Commit fb547f6

Browse files
authored
Completed Arrays Part 2
1 parent 1d9a38b commit fb547f6

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

arrays/exercises/part-two-arrays.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
22

3-
//1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
3+
// 1) Use bracket notation to replace ‘slinky’ with ‘space tether’. Print the array to confirm the change.
4+
cargoHold[5] = 'space tether';
5+
console.log(cargoHold); // ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', 'security blanket']
46

5-
//2) Remove the last item from the array with pop. Print the element removed and the updated array.
7+
// 2) Remove the last item from the array with pop. Print the element removed and the updated array.
8+
let removedItem = cargoHold.pop();
9+
console.log(removedItem); // 'security blanket'
10+
console.log(cargoHold); // ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether']
611

7-
//3) Remove the first item from the array with shift. Print the element removed and the updated array.
12+
// 3) Remove the first item from the array with shift. Print the element removed and the updated array.
13+
let firstItemRemoved = cargoHold.shift();
14+
console.log(firstItemRemoved); // 'oxygen tanks'
15+
console.log(cargoHold); // ['space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether']
816

9-
//4) Unlike pop and shift, push and unshift require arguments inside the (). Add the items 1138 and ‘20 meters’ to the the array - the number at the start and the string at the end. Print the updated array to confirm the changes.
17+
// 4) Add the items 1138 and ‘20 meters’ to the array - the number at the start and the string at the end. Print the updated array to confirm the changes.
18+
cargoHold.unshift(1138);
19+
cargoHold.push('20 meters');
20+
console.log(cargoHold); // [1138, 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', '20 meters']
1021

11-
//5) Use a template literal to print the final array and its length.
22+
// 5) Use a template literal to print the final array and its length.
23+
console.log(`The array ${cargoHold} has a length of ${cargoHold.length}.`);
24+
// The array [1138, 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', '20 meters'] has a length of 7.

0 commit comments

Comments
 (0)