TL ; DR:
splice(i, 0, x) puts x at index i. Use splice(i, 1) to take out the item at index i. The other items move to make room or to close the gap.
let colors = ["red", "green", "blue"];
colors.splice(1, 0, "pink");
// colors is now ["red", "pink", "green", "blue"]
colors.splice(0, 1);
// colors is now ["pink", "green", "blue"]
Full lesson:
push adds an item at the end, and pop takes the last one away. To work anywhere else in an array, use splice.
splice(start, count, item) goes to index start. It takes out count items there. Then it puts in the items you list after count.
With a count of 0, it only inserts. The items from start on move one place to the right:
let pets = ["Rex", "Milo"];
pets.splice(0, 0, "Luna");
// pets is now ["Luna", "Rex", "Milo"]
With a count of 1 and no items, it only removes. The items after it move one place to the left:
pets.splice(0, 1);
// pets is now ["Rex", "Milo"]
Watch the indexes. After an insert or a removal, the items behind that spot have new indexes.
splice changes the array itself. It returns an array of the removed items, like ["Luna"].
Your task
pets holds the pets waiting at the vet, in order.
Bella has an appointment, so she goes second, at index 1. Then Milo goes home. Take him out of the line.
The code below prints how many pets wait once Bella is in. At the end, it prints the pets left.
Expected output:
4
Rex Bella LunaFree plan: 30 tutor messages. PRO is unlimited.
