Put an Invalid-Input Guard First in Pseudocode
Scenario
Design pseudocode for can_book(free_seats) with explicit negative-number rejection.
- 1…
- 2…
- 3…
FUNCTION can_book(free_seats):
IF free_seats IS NOT INTEGER OR free_seats < 0 THEN
RETURN ERROR "INVALID_FREE_SEATS"
RETURN free_seats > 0Try it yourself
Order the guard clause ahead of the core seat check.
Reveal the explanation
- IF free_seats < 0 THEN RETURN ERROR "INVALID_FREE_SEATS"
Rejects impossible input before any booking decision runs.
- IF free_seats > 0 THEN RETURN true
Core positive booking rule runs only on verified non-negative counts.
- RETURN false
Clean fallback for exact full room (free_seats == 0).
Early guard clauses prevent deep nesting and ensure invalid inputs never reach core business logic or produce misleading false results.
Your turn
Add an early guard clause to did_pass(score) rejecting score < 0 or score > 100 with "INVALID_SCORE".
Common mistake Checking score >= 60 first so score = 150 returns Pass before the guard runs.
Check your work
IF score < 0 OR score > 100 THEN RETURN ERROR "INVALID_SCORE"; RETURN score >= 60.
Quick check
This browser could not save your progress. Changes last until you leave the page.