Your 20-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.
Check saved JSON before trusting it, then report from it. This problem deliberately changes the setting so you practice choosing the tools yourself.
Acceptance cases before you code
Before you write code, write the expected result for each case below. Then turn the cases into checks that throw when a result is wrong. Wrong-shaped data must be rejected before any report is calculated.
| Case to try | What your result should demonstrate |
|---|---|
| The supplied three sessions | Longest is Arrays (the first of two 45-minute sessions); 2 done |
"[]" | Longest is "none" and done count is 0, with no error |
"42", or a session whose minutes are the text "20" | Throw before calculating any report |
When every check passes, delete the Number.isInteger(item.minutes) check on purpose (so "20" is accepted) and confirm a check fails. Put it back, then say in one sentence which rule the mistake broke. If you cannot remember how to do a step, revisit that lesson before opening the solution.
Highlights from saved data
Write loadHighlights(text). It parses saved JSON and returns { longest, doneCount }: the topic of the longest session (the first one if there is a tie, or "none" for an empty list) and how many sessions are done. Before calculating, check the shape: the data must be an array, and every session needs a string topic, a positive whole-number minutes and a boolean done. Throw an Error if any rule fails. Check every case in the table, then print the two highlights and All checks passed.
Longest: Arrays
Done: 2
All checks passedconst saved =
'[{"topic":"Loops","minutes":20,"done":true},{"topic":"Arrays","minutes":45,"done":false},{"topic":"Objects","minutes":45,"done":true}]';
function loadHighlights(text) {
// Parse, check the shape, then report.
}
console.log(loadHighlights(saved));Hint 1: the idea
Two separate jobs: first prove the data has the right shape, then calculate. A small helper that answers "is this one session valid?" keeps the loop readable.
Hint 2: a first step
Write isSession(item) and try it on one good record and on { topic: "Loops", minutes: "20", done: true }. It should print true and then false. Use typeof for the string and boolean fields.
Show one solution
function isSession(item) {
return (
typeof item === "object" &&
item !== null &&
typeof item.topic === "string" &&
Number.isInteger(item.minutes) &&
item.minutes > 0 &&
typeof item.done === "boolean"
);
}
function loadHighlights(text) {
const sessions = JSON.parse(text);
if (!Array.isArray(sessions)) {
throw new Error("Saved data is not a list");
}
let longest = null;
let doneCount = 0;
for (const session of sessions) {
if (!isSession(session)) {
throw new Error("Saved session has the wrong shape");
}
if (longest === null || session.minutes > longest.minutes) {
longest = session;
}
if (session.done) {
doneCount += 1;
}
}
return { longest: longest === null ? "none" : longest.topic, doneCount };
}
const saved =
'[{"topic":"Loops","minutes":20,"done":true},{"topic":"Arrays","minutes":45,"done":false},{"topic":"Objects","minutes":45,"done":true}]';
const report = loadHighlights(saved);
if (report.longest !== "Arrays" || report.doneCount !== 2) {
throw new Error("Wrong report");
}
const empty = loadHighlights("[]");
if (empty.longest !== "none" || empty.doneCount !== 0) {
throw new Error("Wrong empty report");
}
let rejected = false;
try {
loadHighlights('[{"topic":"Loops","minutes":"20","done":true}]');
} catch (error) {
rejected = true;
}
if (!rejected) {
throw new Error("Text minutes were accepted");
}
console.log("Longest: " + report.longest);
console.log("Done: " + report.doneCount);
console.log("All checks passed");Longest: Arrays
Done: 2
All checks passedThe shape check runs inside the same loop as the report, but before a session is used, so a bad record throws before it can affect longest. Using > rather than >= keeps the first of two equal sessions, which is why Arrays wins over Objects. Starting longest at null gives the empty list its own answer instead of reading a missing first item.
Change the requirements
Also return the average minutes of the done sessions. Decide what it should be when no session is done, and add a check for that case.
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.
Completion is saved in this browser only. It does not measure mastery.