Checkpoint: Weekly Summary

The Course
•
4 min read

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
30

Acceptance cases before you code

Write a small test plan before implementing readonly report inputs. These cases extend the brief; they are not a second worked solution. Predict the expected behavior independently, then test through the public function. A good ordinary example and a good boundary example should catch different possible mistakes.

Case to tryWhat your result should demonstrate
No sessionsZero completed count and minutes
Unequal durations with mixed done flagsCount records and sum minutes separately
Before and after summaryPreserve every source record

After the core task passes, deliberately break one rule and identify the case that notices it. Restore the implementation and explain the failure in terms of the contract, not the number of assertions. If a case requires a concept you have not recalled, revisit the relevant lesson before opening the solution.

Your turn

Exercise

Checkpoint: Weekly Summary: practise

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.

2
35
0

Explain the types at the function boundary and the runtime checks, if any. Compare your approach before copying this answer.

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.