|
1 | 1 | let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', '20 meters'];
|
2 | 2 |
|
3 |
| -//Use splice to make the following changes to the cargoHold array. Be sure to print the array after each step to confirm your updates. |
| 3 | +// 1) Insert the string 'keys' at index 3 without replacing any other entries. |
| 4 | +cargoHold.splice(3, 0, 'keys'); |
| 5 | +console.log(cargoHold); // [1138, 'space suits', 'parrot', 'keys', 'instruction manual', 'meal packs', 'space tether', '20 meters'] |
4 | 6 |
|
5 |
| -//1) Insert the string 'keys' at index 3 without replacing any other entries. |
| 7 | +// 2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index). |
| 8 | +let indexToRemove = cargoHold.indexOf('instruction manual'); |
| 9 | +if (indexToRemove !== -1) { |
| 10 | + cargoHold.splice(indexToRemove, 1); |
| 11 | +} |
| 12 | +console.log(cargoHold); // [1138, 'space suits', 'parrot', 'keys', 'meal packs', 'space tether', '20 meters'] |
6 | 13 |
|
7 |
| -//2) Remove ‘instruction manual’ from the array. (Hint: indexOf is helpful to avoid manually counting an index). |
8 |
| - |
9 |
| -//3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’. |
| 14 | +// 3) Replace the elements at indexes 2 - 4 with the items ‘cat’, ‘fob’, and ‘string cheese’. |
| 15 | +cargoHold.splice(2, 3, 'cat', 'fob', 'string cheese'); |
| 16 | +console.log(cargoHold); // [1138, 'space suits', 'cat', 'fob', 'string cheese', 'space tether', '20 meters'] |
0 commit comments