Skip to content

Checkpoint: Parser Tests

The Course
•
20 min study

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.

Parse a new text format and test its boundaries. 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 next to it. Then turn the cases into checks that throw when a result is wrong, as in Testing Your Code. Boundaries such as 4:00 and 4:01 catch different mistakes from ordinary times.

Case to tryWhat your result should demonstrate
"1:30", " 0:05 " and "4:00"Accept ordinary, padded and largest-allowed durations: 90, 5 and 240
"4:01", "0:00" and "1:60"Reject values outside 1–240 minutes, and a minutes part above 59
"1:", "90", "1:30:00" and "1.5:00"Reject a blank part, a missing or extra colon, and a fraction

When every check passes, change minutes > 59 to minutes > 60 on purpose 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.

Exercise

A duration parser with evidence

Write parseDuration(text) for durations typed as hours and minutes, such as "1:30". Return the total number of minutes, or null when the text breaks a rule: exactly one colon, both parts whole numbers, minutes from 0 to 59, and a total from 1 to 240 minutes. Surrounding spaces are allowed. Write a check for every case in the table that throws if a result is wrong, then print how many checks passed.

Your working space JavaScript
function parseDuration(text) {
  // Return total minutes, or null.
}

// One check per case in the table.
console.log(parseDuration("1:30"));
Hint 1: the idea

Check the structure first, then convert each part, then apply the range rules. Remember that Number("") is 0, so an empty part must be rejected before converting.

Hint 2: a first step

Start with const parts = text.trim().split(":"); and return null unless there are exactly two parts. Print parts for "1:30" and "1:30:00" to see the difference.

Show one solution
JavaScript
function wholeOrNull(text) {
  if (text.trim() === "") {
    return null;
  }
  const value = Number(text);
  return Number.isInteger(value) ? value : null;
}

function parseDuration(text) {
  const parts = text.trim().split(":");

  if (parts.length !== 2) {
    return null;
  }

  const hours = wholeOrNull(parts[0]);
  const minutes = wholeOrNull(parts[1]);

  if (hours === null || minutes === null || hours < 0 || minutes < 0 || minutes > 59) {
    return null;
  }

  const total = hours * 60 + minutes;

  if (total < 1 || total > 240) {
    return null;
  }

  return total;
}

const cases = [
  { input: "1:30", expected: 90 },
  { input: " 0:05 ", expected: 5 },
  { input: "4:00", expected: 240 },
  { input: "4:01", expected: null },
  { input: "0:00", expected: null },
  { input: "1:60", expected: null },
  { input: "1:", expected: null },
  { input: "90", expected: null },
  { input: "1:30:00", expected: null },
  { input: "1.5:00", expected: null }
];

for (const testCase of cases) {
  const actual = parseDuration(testCase.input);

  if (actual !== testCase.expected) {
    throw new Error(`Failed: "${testCase.input}" gave ${actual}`);
  }
}

console.log(`${cases.length} checks passed`);
Text
10 checks passed

wholeOrNull handles blank text and fractions once, so both parts follow the same rule. Range checks come only after both parts are known to be whole numbers. The total is checked separately from the minutes part, because 4:01 has a valid minutes part but too large a total. Without the blank check, "1:" would become 1 hour and 0 minutes and be accepted as 60.

Change the requirements

Allow a plain number of minutes such as "45" as well as H:MM. Add the cases that prove both forms still follow the 1–240 rule.

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.