Checkpoint: Parser Tests

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.

Specify a contract and test its boundaries. 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(Number.isInteger(Number("2.5")));
Check your prediction
false

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 whole-number input boundaries. 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
Text 1 and 12Accept both endpoints
Blank, zero, 13 and fractionReject for the stated rule rather than coercing a fallback
Padded valid textNormalize before conversion and accept the whole number

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 parser with evidence

Write parseCount(text) for whole numbers from 1 through 12. Return null for blank, invalid, or out-of-range text. Write checks for 1, 12, 0, 13, spaces, and a decimal. Throw if any result is wrong.

Your working space JavaScript
function parseCount(text) {
  // Validate a string input.
}
// Write independent expected values.
Hint 1: the idea

Check blank text before converting and check both ends inclusively.

Hint 2: a first step

Use Number.isInteger after Number; compare each returned result with a hand-written expectation.

Show one solution
function parseCount(text) {
  if (text.trim() === "") {
    return null;
  }

  const count = Number(text);

  if (!Number.isInteger(count) || count < 1 || count > 12) {
    return null;
  }

  return count;
}

const cases = [
  { input: "1", expected: 1 },
  { input: "12", expected: 12 },
  { input: "0", expected: null },
  { input: "13", expected: null },
  { input: "  ", expected: null },
  { input: "1.5", expected: null }
];

for (const testCase of cases) {
  if (parseCount(testCase.input) !== testCase.expected) {
    throw new Error(`Failed: ${testCase.input}`);
  }
}

console.log("6 checks passed");
6 checks passed

Temporarily replace > 12 with >= 12. Confirm your boundary check fails, then restore it. Add “twelve” and ” 5 ” cases yourself.

Change the requirements

Temporarily replace > 12 with >= 12. Confirm your boundary check fails, then restore it. Add “twelve” and ” 5 ” cases yourself.

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.