|
1 | 1 | let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];
|
2 | 2 |
|
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'] |
4 | 6 |
|
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'] |
6 | 11 |
|
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'] |
8 | 16 |
|
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'] |
10 | 21 |
|
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