Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Porting the Monday Rota Script

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) is 8.
  • 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 to pop(), 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 in before remove() takes Sam out. JavaScript has no remove(), 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, the else branch logs the message instead. Without that check, .splice() would read -1 as 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() becomes console.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 with rota.includes(asker). JavaScript’s in operator 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’s sorted(). JavaScript’s sort returns the array it reorders, so it can pass for sorted(). 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:

Language: JavaScript
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(", ")}`);

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

00:02 The team’s tooling is moving to JavaScript, so you’ll port the team lead’s Monday script, and it must post exactly what the Python posts. The Python script rotates the rota, takes out anyone on leave, and adds the new hire.

00:18 Then it prints the week for the team channel, answers Kofi, and lists the rota for the wiki. The last three lines report the pager. They stay folded: you’ll port them in the exercise.

00:31 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. In Python, the comma makes the tuple, but in JavaScript, it keeps only the last value, the 8.

00:49 The new hire keeps its capitals, as Python’s constants do. The leave request becomes camel case, and the asker, Kofi, keeps his name.

01:00 The rotation is next. An array has no append(), so append() becomes push(). Python passes position 0 to pop(), but JavaScript’s pop() takes no position, so the front has its own method, shift().

01:15 Together, push() of shift() moves Marta to the back.

01:21 Sam is on leave this week. The Python script checks the name with in before remove() takes him out. JavaScript has no remove(), but indexOf() answers both questions: whether Sam is there, and where.

01:37 The port keeps that index and splices only when it isn’t -1. When it is -1, the message logs instead. Without that check, splice() would read -1 as the last person, Marta.

01:53 Lena, the new hire, goes in after this week’s person, where Python inserts at position 1. The same splice() that took Sam out puts her in.

02:03 Start at 1, remove nothing, and put in the new hire.

02:10 Now the team channel’s lines. Each print() becomes console.log(), and each f-string a template literal. This week’s person is still at position 0.

02:21 For the next three weeks, slice() takes 0 to 3, the end left out. join() is called on the array, with the separator passed in.

02:31 The out-of-hours line reads the pair by position, as the tuple was read.

02:37 Kofi asked whether he’s on the rota. Python asks with in, and JavaScript with includes(). JavaScript’s in operator asks about keys, such as an array’s positions, not its values.

02:51 With it, Kofi would get no answer.

02:56 For the wiki, Python’s sorted() copies the rota alphabetically. JavaScript’s copy is toSorted(). JavaScript’s sort() returns the array it reorders, so it can pass for sorted().

03:09 But it reorders the rota too: next Monday would start from Bea. The last line keeps the real order.

03:19 Here’s the whole port, next to the Python it came from: the settings, rotation, leave, new hire, channel, Kofi and wiki.

03:31 First, node --check reads the file, and says nothing: no syntax errors. Run it, and JavaScript prints six lines. They match Python’s first six, word for word.

03:47 Now change the request to Rui in both scripts. Which three names does the next three weeks line print now? Your turn: pause the video and predict.

04:02 Run them: Dev, Lena, Sam. Sam stays, and Lena goes in ahead of him. Both print the message first, and Marta is still last. Then the name goes back to Sam.

04:19 So here’s the rule: port one step at a time, each the JavaScript way, then check that the output matches. And not found is -1, checked before you splice.

04:32 Next up is the exercise Finish the Pager Report. You’ll port the pager lines: last week’s page count and the three busiest weeks, with the counts left in week order.

Become a Member to join the conversation.