Build a reusable label function for a study session. This time the result must be returned to the caller, so it could later be displayed on a page or included in a larger report. Printing inside the function would tie it to one output mechanism.
Plan the contract first: what is the record called, which properties are required, which one is optional, and what does the function return? Write the answers in comments before implementation.
Your function should work for any valid record of that shape, not just the example. A useful check is to create a second record with different values without editing the function. Use the compiler to catch mismatched fields, then use the output to check the formatting rule.
Keep this checkpoint to the features already introduced. You do not need classes, assertions, generics or a special framework. Reuse ordinary JavaScript template literals and the fallback operator from the previous lesson.
Predict, check, then run
Before running, write down the output and explain which information the checker knows. Click Check types first, then Check & run.
type Session = {
topic: string;
minutes: number;
};
function total(session: Session, count = 1): number {
return session.minutes * count;
}
console.log(total({ topic: "Records", minutes: 15 }, 2));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 required and optional session fields. 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 try | What your result should demonstrate |
|---|---|
| Missing optional note | Use the documented fallback |
| Present empty note | Follow the deliberately chosen empty-text policy |
| Wrong required field type | The public caller fails checking |
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
Checkpoint: Session Labels: practise
Create Session with topic:string, minutes:number and optional note:string. Write sessionLabel returning “Topic: N min — Note”; absent note uses “No note”. Print Types: 25 min — No note and Loops: 10 min — Review.
// Define the contract, implement sessionLabel, and check two records.Enable JavaScript to edit and type-check this example.
Hint 1: the idea
The function should have one record parameter and return a string.
Hint 2: a first step
Compute the note fallback separately if the template literal becomes hard to read.
Show one solution
type Session = {
topic: string;
minutes: number;
note?: string;
};
function sessionLabel(session: Session): string {
const note = session.note ?? "No note";
return `${session.topic}: ${session.minutes} min — ${note}`;
}
console.log(sessionLabel({ topic: "Types", minutes: 25 }));
console.log(sessionLabel({ topic: "Loops", minutes: 10, note: "Review" }));Enable JavaScript to edit and type-check this example.
Types: 25 min — No note
Loops: 10 min — ReviewExplain 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 an empty note and zero minutes. Explain whether either should be rejected and where such a decision belongs. Then deliberately pass minutes as text and use the diagnostic to locate the broken contract. Redo the task tomorrow with book records instead of sessions.
Completion is saved in this browser only. It does not measure mastery.