Building the Rota as an Array

Every Monday morning, the support lead runs monday_rota.py, and everyone learns who’s carrying the pager this week through the team channel. Everything that script does, from rotating the queue to sorting it for the HR wiki, starts from one Python list of six names. Before the JavaScript version can move anyone around, it needs that list.

In this lesson, you’ll build the rota as a JavaScript array, read both ends of it, and count its items. Most of your list habits carry over. The exception is what happens when you ask for a position that isn’t there. Start with the rota as it stood last Monday.

Write the Rota and Read Its Ends

The Python script keeps the rota in queue order, with last week’s person first. It reads the front of the queue with [0] and the back with [-1]:

Language: Python
rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"]
print(f"Last week: {rota[0]}")
print(f"Back of the queue: {rota[-1]}")

In the JavaScript port, the list becomes an array, JavaScript’s general-purpose list type. The literal uses the same square brackets and commas. With const, you can’t reassign the name rota to a different array, but the array itself can still change, which Monday’s rotation relies on. Template literals fill in for the f-strings:

Language: JavaScript
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
console.log(`Last week: ${rota[0]}`);
console.log(`Back of the queue: ${rota.at(-1)}`);

Run it, and you get the same two lines as the Python script:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.