The support team’s Monday script starts with six names in queue order, last week’s pager carrier first. Before you port the rotation, you need to read both ends and count the people. JavaScript uses an array literal with the familiar square brackets of a Python list literal, but reading a stale position can let an invalid value reach the team channel.
Read the Queue
JavaScript’s array fills the role of Python’s list, with the same square bracket literal and zero-based indexing. These are the array operations the Monday report needs:
| Python | JavaScript |
|---|---|
rota[0] |
rota[0] |
rota[-1] |
rota.at(-1) |
len(rota) |
rota.length |
As with strings, a negative index passed to .at() counts from the end. JavaScript exposes the size as a property, .length, so you read it without parentheses rather than calling a general function like Python’s len().
Build last Monday’s rota and print its ends and size, using template literals, JavaScript’s f-string equivalent with backticks and ${...} placeholders:
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
console.log(`Last week: ${rota[0]}`);
console.log(`Back of the queue: ${rota.at(-1)}`);
console.log(`${rota.length} people share the pager`);
The report identifies Marta and Bea:
Last week: Marta
Back of the queue: Bea
6 people share the pager
A const declaration prevents reassignment of rota, but the array itself can change. For Python’s isinstance(rota, list), use Array.isArray(rota), because typeof rota reports "object".
Watch out: rota[-1] returns undefined, as with strings, because brackets look up the key -1 rather than counting backward, so use .at(-1).
Writing rota[-1] = "Rui" adds a stray "-1" property instead of replacing Bea and leaves .length at six, so replace the last item by assigning to rota[rota.length - 1] once you know the array isn’t empty.
Check the Queue Length
Rui used to be seventh before moving to the daytime team. Python’s old rota[6] read would raise IndexError today. JavaScript treats an out-of-range position as a missing property and returns undefined, its value for “no value,” instead.
Read the stale position and the back of an empty rota:
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
const emptyRota = [];
console.log(`Seventh in the queue: ${rota[6]}`);
console.log(`Back of an empty rota: ${emptyRota.at(-1)}`);
console.log(`Empty rota length: ${emptyRota.length}`);
Both reads let the script carry on:
Seventh in the queue: undefined
Back of an empty rota: undefined
Empty rota length: 0
Check .length before reading a name. A non-negative integer position must be smaller than .length, and reading the last name with .at(-1) requires a non-empty rota. For the rota of names shown here, these checks keep undefined out of the channel message without relying on Python’s exception behavior.
For the channel report, use the familiar if ... else to read the last name only when the rota isn’t empty:
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
if (rota.length > 0) {
console.log(`Back of the queue: ${rota.at(-1)}`);
} else {
console.log("The rota is empty; arrange pager coverage.");
}
With this rota, the script reports Bea:
Back of the queue: Bea
With const rota = []; instead, the same check gives the team a useful message:
The rota is empty; arrange pager coverage.
In the next lesson, Rotating the Rota With push() and shift(), you’ll move Marta from the front to the back for Monday’s handover, leaving Dev first in the queue.