Removing Elements (pop, shift)

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

β–Ά Do the exercises

Exercise 1 β€” Removing the Last: pop

`pop()` does the opposite of `push()`: it removes the last element. And it gives it back to you, so you can use it:

const bag = ["Potion", "Map", "Elixir"]; let used = bag.pop(); console.log("You used: " + used); console.log(bag);

The array now has 2 elements, and `used` holds the one that was removed. No argument needed: `pop` always takes the last one.

Exercise 2 β€” Removing the First: shift

`shift()` removes the first element and shifts all the others one position to the left. Like `pop`, it returns the removed element:

const line = ["Sam", "Lea", "Noa"]; let next = line.shift(); console.log("Now serving: " + next); console.log(line); console.log(line[0]);

Sam is served, Lea is now at index 0. This is exactly how a queue works: first in, first out.

Exercise 3 β€” Quiz: After pop

What does this code print?

const team = ["Aquachu", "Pyrochu", "Florachu"]; team.pop(); console.log(team.length);
Options
β€’ 2
β€’ 3
β€’ 1
β€’ "Florachu"

Hint : The question is about the size of the array, not about what was removed.

Exercise 4 β€” Use and Serve

Remove the last item of the bag and the first trainer of the line, storing each removed value.

Hint : One method per end of the array, and both give the removed element back.

Exercise 5 β€” Popping an Empty Array

What if there's nothing left to remove? No error: `pop` and `shift` simply return `undefined` and the array stays empty.

const bag = ["Potion"]; console.log(bag.pop()); console.log(bag.pop()); console.log(bag.length);

So before using a removed value, it's a good habit to check `length` first, or to test the result against `undefined`:

const bag = []; let item = bag.pop(); if (item === undefined) { console.log("The bag is empty!"); } else { console.log("You used: " + item); }

Exercise 6 β€” Quiz: Nothing Left

What does this code print?

const moves = []; console.log(moves.shift());
Options
β€’ undefined
β€’ An error
β€’ null
β€’ 0

Hint : Removing from an empty array is allowed. What does "nothing" look like in JavaScript?

Exercise 7 β€” Emptying an Array with a Loop

Combine `length` and a `while` loop to process elements one by one until nothing is left. Here every trainer in line gets served:

const line = ["Sam", "Lea", "Noa"]; while (line.length > 0) { let trainer = line.shift(); console.log("Serving " + trainer + "..."); } console.log("Everyone has been served!");

Each `shift` makes the array shorter, so the loop ends by itself. With `pop` instead, the elements would come out in reverse order: last in, first out, like a stack of plates.

Exercise 8 β€” Drink Them All

Use all the potions in the bag, starting with the last one, until the bag is empty.

Hint : The loop must keep going as long as something remains, and each turn must take from the correct end.

Exercise 9 β€” pop vs shift

Match each element with its effect.

Items to match
β€’ pop()
β€’ shift()
β€’ The value returned by `pop()` and `shift()`
β€’ [].pop()
Definitions
β€’ Removes the last element
β€’ Removes the first element
β€’ The removed element
β€’ undefined

Exercise 10 β€” Battle Preparation

Before the battle: 1. Use the last two items of `bag`, printing `"Used: X"` for each one 2. The first trainer in `line` gets called: remove them and print `"Called: X"` 3. Print `"Items left: N"` and `"Next in line: Y"`

Starter code
const bag = ["Berry", "Potion", "Super Potion", "Elixir"]; const line = ["Sam", "Lea", "Noa"]; // Write your code here
Back to the course