Adding Up Booked Time With Duration

With the reminders a calendar day ahead, the report’s last two lines show each physiotherapist’s booked time. Python adds timedelta(minutes=appt["minutes"]) per clinician with booked.get(clinician, timedelta()) + ... and prints Isla Grant: 1:40:00 booked and Callum Reid: 1:20:00 booked. Four sessions count because Fiona’s Tuesday appointment falls on the closed day and Jonas’s Thursday appointment falls outside the week.

Balance a Duration Before You Show It

The port keeps a Map of Temporal.Duration values. new Temporal.Duration() corresponds to Python’s timedelta(), and .add({ minutes }) returns a new duration.

The Map keys are clinician names. .get() reads the existing total, ?? supplies an empty duration when there’s none, and .set() saves the new total. In the display loop, [clinician, total] unpacks each entry into its key and value.

For display, Intl.DurationFormat with style: "digital" produces clock-like text. The "long" style gives 1 hour, 40 minutes. With "en-GB", balanced positive totals below a day match str(timedelta). Python prints 1:40:00 for Isla. Predict what the port prints:

Language: JavaScript
const sessions = [
  { clinician: "Isla Grant", minutes: 50 },
  { clinician: "Isla Grant", minutes: 50 },
  { clinician: "Callum Reid", minutes: 50 },
  { clinician: "Callum Reid", minutes: 30 },
];
const booked = new Map();
const digital = new Intl.DurationFormat("en-GB", {
  style: "digital",
});

for (const appt of sessions) {
  const clinician = appt.clinician;
  const total = booked.get(clinician) ?? new Temporal.Duration();
  booked.set(clinician, total.add({ minutes: appt.minutes }));
}

for (const [clinician, total] of booked) {
  console.log(`${clinician}: ${digital.format(total)} booked`);
}

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.