Porting the Monday Rota Script
The team’s tooling is moving to JavaScript, so in this project, you ported the team lead’s Monday script, and it posts exactly what the Python posts: the same six lines. The pager lines stay folded: you’ll port them in the exercise.
- The rota carries over as an array in a
const, which fixes the name, not the contents. The out-of-hours window gets square brackets,[18, 8]. In Python, the comma makes the tuple, but in JavaScript, it keeps only the last value, so(18, 8)is8. - The new hire keeps its capitals,
NEW_HIRE, as Python’s constants do. The leave request becomes camel case,onLeave, and the asker, Kofi, keeps his name. - An array has no
append(), so append becomes.push(). Python passes position zero topop(), but JavaScript’s pop takes no position, so the front has its own method,.shift(). Together,rota.push(rota.shift())moves Marta to the back. - The Python script checks the name with
inbeforeremove()takes Sam out. JavaScript has noremove(), but.indexOf()answers both questions: whether Sam is there, and where. The port keeps that index and splices only when it isn’t-1. - When it is
-1, theelsebranch logs the message instead. Without that check,.splice()would read-1as the last person, Marta. - Lena goes in after this week’s person, where Python inserts at position one. The same
splice that took Sam out puts her in:
rota.splice(1, 0, NEW_HIRE)starts at one, removes nothing, and puts in the new hire. - Each
print()becomesconsole.log(), and each f-string a template literal. This week’s person is still at position zero,rota[0]. - For the next three weeks,
rota.slice(0, 3)takes zero to three, the end left out..join(", ")is called on the array, with the separator passed in. The out-of-hours line reads the pair by position, as the tuple was read. - Python asks whether Kofi is on the rota with
in, and JavaScript withrota.includes(asker). JavaScript’sinoperator asks about keys, such as an array’s positions, not its values, so with it Kofi would get no answer. - The wiki list is
rota.toSorted(), a sorted copy, like Python’ssorted(). JavaScript’s sort returns the array it reorders, so it can pass forsorted(). But it reorders the rota too: next Monday would start from Bea. The last line keeps the real order.
Here’s the whole port, monday-rota.js, with the pager lines still to come:
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
const ON_CALL_HOURS = [18, 8];
const NEW_HIRE = "Lena";
const onLeave = "Sam";
const asker = "Kofi";
rota.push(rota.shift());
const index = rota.indexOf(onLeave);
if (index !== -1) {
rota.splice(index, 1);
} else {
console.log(`${onLeave} isn't on the rota`);
}
rota.splice(1, 0, NEW_HIRE);
console.log(`On call this week: ${rota[0]}`);
console.log(`Next three weeks: ${rota.slice(0, 3).join(", ")}`);
console.log(
`Out of hours: ${ON_CALL_HOURS[0]}:00 to ${ON_CALL_HOURS[1]}:00`,
);
if (rota.includes(asker)) {
console.log(`${asker} is on the rota.`);
}
const wiki = rota.toSorted();
console.log(`Wiki list: ${wiki.join(", ")}`);
console.log(`Next Monday starts from: ${rota.join(", ")}`);