.slice() and .splice()

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Taking a Piece of an Array

Imagine you have a squad of four Codémon in your backpack, but a tournament battle only allows you to send in your first two Codémon. You want to extract those two into a new array without modifying your full team.

The `.slice(start, end)` method copies a portion of an array and returns it as a brand-new array:

const team = ["Aquachu", "Pyrochu", "Zap", "Florachu"]; const fighters = team.slice(0, 2); console.log(fighters); console.log(team);

Notice two important rules: - The `start` index (`0`) is included, while the `end` index (`2`) is excluded (it stops right before index `2`, so it grabs indexes `0` and `1`). - The original `team` array is never modified.

Exercise 2 — Shortcuts and Negative Indexes

You don't always have to pass two numbers to `.slice()`: - If you only give the `start` index, `.slice(start)` grabs everything from that index all the way to the end. - If you pass no arguments at all, `.slice()` makes a complete shallow copy of the entire array.

const team = ["Aquachu", "Pyrochu", "Zap"]; console.log(team.slice(1)); console.log(team.slice());

You can also pass a negative number to count backwards from the end of the array. For example, `slice(-2)` grabs the last 2 elements, and `slice(-1)` grabs the very last element:

const team = ["Aquachu", "Pyrochu", "Zap", "Florachu"]; console.log(team.slice(-2)); console.log(team.slice(-1));

Exercise 3 — Quiz: Which Slice?

What does this code print?

const nums = [10, 20, 30, 40]; console.log(nums.slice(1, 3));
Options
• 20,30
• 20,30,40
• 10,20,30
• 30

Hint : The first index joins the piece, the second one marks where to stop.

Exercise 4 — The Top Three

Keep the first three results.

Hint : One method copies a section without touching the original, and the end index is not included.

Exercise 5 — splice Changes the Original

`splice` looks similar to `slice` but does the opposite: it modifies the array in place, removing elements and returning the removed ones. You give it a starting index and how many to remove:

const team = ["Aquachu", "Pyrochu", "Zap"]; const removed = team.splice(1, 1); console.log(removed); console.log(team);

Pyrochu was pulled out of the array, and `team` is now shorter. This is a real change, not a copy.

Exercise 6 — splice Can Insert Too

`.splice()` can also insert new elements into the array! To do this, you pass a third argument right after the number of elements to remove:

array.splice(startIndex, deleteCount, newElement);

1. Insert without removing (`deleteCount` is `0`) Here, `team.splice(1, 0, "Pyrochu")` goes to index `1`, removes `0` elements, and inserts `"Pyrochu"` at index `1` (sliding `"Zap"` to the right):

const team = ["Aquachu", "Zap"]; team.splice(1, 0, "Pyrochu"); console.log(team);

2. Replace an element (`deleteCount` is `1`) Here, `team.splice(1, 1, "Florachu")` goes to index `1`, removes `1` element (`"Pyrochu"`), and inserts `"Florachu"` in its exact spot:

const team = ["Aquachu", "Pyrochu", "Zap"]; team.splice(1, 1, "Florachu"); console.log(team);

Exercise 7 — Quiz: After the splice

What does this code print?

const nums = [1, 2, 3, 4]; nums.splice(1, 2); console.log(nums);
Options
• 1,4
• 2,3
• 1,2,3,4
• 1,2

Hint : Start at that index and take out that many elements.

Exercise 8 — Remove the Second

Remove exactly one element, the one at index 1, modifying the array itself.

Hint : One method modifies the original, and the second argument says how many to take out.

Exercise 9 — slice vs splice

Match each element with its description.

Items to match
• slice(a, b)
• splice(a, n)
• slice()
• splice(a, 0, x)
Definitions
• A new array, original untouched
• Modifies the array, returns what was removed
• A full copy
• Inserts x without removing

Exercise 10 — Team Rotation

Manage the roster between two battles, using the right method for each job.

Step 1: build `active`, a copy of the first three members, without touching `roster`. Print it and print `"Roster still: N"`. Step 2: Pyrochu, at index 1 of `roster`, is injured: remove it from `roster` and print `"Out: X"` with the removed name. Step 3: insert `"Florachu"` at index 1 of `roster`, without removing anything. Step 4: print the final roster and `"Last two: X"` with a copy of its last two members.

Expected output:

Aquachu,Pyrochu,Zap Roster still: 4 Out: Pyrochu Aquachu,Florachu,Zap,Metachu Last two: Zap,Metachu
Starter code
const roster = ["Aquachu", "Pyrochu", "Zap", "Metachu"]; // Write your code here
Back to the course