Checkpoint: Saved Reports

The Course
•
4 min read

Your 15-minute checkpoint

Close the earlier examples. Spend two minutes writing the inputs, expected outputs, and steps in plain language. Use about eight minutes to build and test, then five to explain your choices and try the variation. Taking longer is normal: these are study budgets, not speed tests.

Combine JSON, records, and testable reporting. This problem deliberately changes the setting so you practise choosing the tools yourself.

A small warm-up

Before the main task, predict this familiar idea from today’s work. If it is unclear, revisit the relevant teaching lesson first.

Check the foundation

Say why each line appears, not just what it prints.

Check the foundation JavaScript
console.log(JSON.parse("[1,2]").length);
Check your prediction
2

Use this only as a warm-up. The main task needs you to combine several decisions.

Acceptance cases before you code

Write a small test plan before implementing trusted json and report behavior. 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
Empty session arrayReturn zero planned and completed totals
All sessions completeCompleted equals planned
Different session durationsCalculate minutes rather than completed record count

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.

Exercise

A planner report from saved data

Parse the supplied trusted JSON, then write summarize(sessions) returning planned and completed minutes. Do not change the records. Check both the supplied example and an empty array.

Your working space JavaScript
const saved =
  '[{"topic":"Loops","minutes":20,"done":true},{"topic":"Arrays","minutes":30,"done":false}]';
Hint 1: the idea

Keep two accumulators: all minutes and completed minutes.

Hint 2: a first step

Only add to completed when the done flag is true.

Show one solution
function summarize(sessions) {
  let planned = 0;
  let completed = 0;

  for (const item of sessions) {
    planned += item.minutes;

    if (item.done) {
      completed += item.minutes;
    }
  }

  return { planned, completed };
}

const saved =
  '[{"topic":"Loops","minutes":20,"done":true},{"topic":"Arrays","minutes":30,"done":false}]';
const report = summarize(JSON.parse(saved));

if (report.planned !== 50 || report.completed !== 20) {
  throw new Error("Wrong report");
}

const empty = summarize([]);

if (empty.planned !== 0 || empty.completed !== 0) {
  throw new Error("Wrong empty report");
}

console.log("Planned: " + report.planned);
console.log("Completed: " + report.completed);
Planned: 50
Completed: 20

This input is trusted practice data. Describe the shape validation you would add before accepting a file from another person. Try all done and none done.

Change the requirements

This input is trusted practice data. Describe the shape validation you would add before accepting a file from another person. Try all done and none done.

Decide what to revisit

  • Can you explain each variable and the reason for each condition?
  • Did you check an empty, boundary, or changed input rather than only the supplied one?
  • Can you rebuild the essential part after closing the solution?

If you needed a hint, record the specific missing idea and revisit that lesson. Marking a page done records progress; it does not replace practice. Begin tomorrow by rebuilding one small part from memory.

Was this article helpful?

Your response is saved on this device.