Skip to content

Commit 43c2ba7

Browse files
authored
Completed part three
1 parent fb547f6 commit 43c2ba7

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

arrays/exercises/part-three-arrays.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
let cargoHold = [1138, 'space suits', 'parrot', 'instruction manual', 'meal packs', 'space tether', '20 meters'];
22

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']
46

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']
613

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

Comments
 (0)