Skip to content

Checkpoint: Weekly Summary

The Course
•
25 min study

Create a report from session records. The report function should count only completed sessions and should not edit or reorder the input. This combines array element types, readonly contracts, iteration and a fresh result object.

Write the input and output shapes first. A report has two numeric fields: completedCount and completedMinutes. Naming both makes the caller's job clearer than returning an unexplained pair of numbers.

During implementation, keep the accumulated values local to the function. Do not reach into a global array. The function should give the same answer for the same input, including an empty list. This makes it easier to test and reuse.

Before opening a hint, manually calculate the expected report for the supplied records. Then check the compiler and run the code. Include the unchanged input in your observations: readonly declarations communicate the rule, while an output comparison checks the behavior of this implementation.

Predict, check, then run

Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.

Predict, then runTypeScript
const minutes: readonly number[] = [10, 20, 0];
let total = 0;

for (const value of minutes) {
  total += value;
}

console.log(total);

Enable JavaScript to edit and type-check this example.

Check your prediction
Text
30

Acceptance cases before you code

Before you write the summary, work out the count and minutes each input below should produce.

Case to tryWhat your result should demonstrate
No sessionsZero completed count and minutes
Unequal durations with mixed done flagsCount records and sum minutes separately
Keep a reference to the input list and its recordsSummarizing neither sorts the input nor changes its fields

Try adding minutes for every session instead of only done ones: the empty list still passes, and only the mixed-flags case shows the wrong total. Try calling sessions.sort() inside the function: the readonly parameter type rejects it before any case runs.

Your turn

Exercise

Checkpoint: Weekly Summary: practice

Define Session with readonly topic, minutes and done properties. Write summarize(sessions: readonly Session[]) returning Report with completedCount and completedMinutes. For completed Types/25, incomplete DOM/40 and completed Loops/10, print 2 and 35. Also print 0 for an empty list’s completedMinutes.

Your workspaceTypeScript
// Define the shapes, implement the accumulator, and test full and empty lists.

Enable JavaScript to edit and type-check this example.

Hint 1: the idea

Only add a record to both totals when its done field is true.

Hint 2: a first step

Initialize both counters to zero inside summarize and update them in a for/of loop.

Show one solution
One possible solutionTypeScript
type Session = {
  readonly topic: string;

  readonly minutes: number;

  readonly done: boolean;
};

type Report = {
  completedCount: number;

  completedMinutes: number;
};

function summarize(sessions: readonly Session[]): Report {
  let completedCount = 0;
  let completedMinutes = 0;

  for (const session of sessions) {
    if (session.done) {
      completedCount += 1;
      completedMinutes += session.minutes;
    }
  }

  return { completedCount, completedMinutes };
}

const sessions: Session[] = [
  { topic: "Types", minutes: 25, done: true },
  { topic: "DOM", minutes: 40, done: false },
  { topic: "Loops", minutes: 10, done: true }
];
const report = summarize(sessions);

console.log(report.completedCount);
console.log(report.completedMinutes);
console.log(summarize([]).completedMinutes);

Enable JavaScript to edit and type-check this example.

Text
2
35
0

readonly Session[] plus readonly fields means summarize cannot sort, push or edit what it was given; the checker would reject any of those. The Report type names both numbers, so a caller reads report.completedMinutes instead of guessing which half of a pair is which. Both counters change only inside if (session.done), which is why DOM's 40 minutes are left out and the empty list reports 0.

Change it and explain it

Test all-incomplete records and a completed zero-minute record. Check that the original list still has the same values and order. Explain why a readonly array alone would not have made mutable record properties readonly.

Was this article helpful?

Your response is saved on this device.