Check the Same Inputs in Two Languages
Scenario
The booking function accepts 1 or more free seats. Run the same checks in Python and JavaScript.
# booking.py
def can_book(free_seats: int) -> bool:
return free_seats > 0
assert can_book(1)
assert not can_book(0)
print("OK")
# python3 booking.py// booking.mjs
import assert from "node:assert/strict";
function canBook(freeSeats) {
return freeSeats > 0;
}
assert.equal(canBook(1), true);
assert.equal(canBook(0), false);
console.log("OK");
// node booking.mjsTry it yourself
Predict what both implementations should return.
| Case | Result |
|---|---|
| 1 free seat | Select a choice above to see what happens. |
| 0 free seats | Select a choice above to see what happens. |
| 2 free seats | Select a choice above to see what happens. |
Reveal the explanation
- 1 free seat
Both return their language’s true boolean.
- 0 free seats
Both return their language’s false boolean.
- 2 free seats
Both should return true. Add this case to each set of checks.
Treat the expected cases as a shared contract. After changing one implementation, rerun the same cases in both.
Shared booking contract
Accept a booking when at least one seat is free.
| Free seats | Expected |
|---|---|
| 0 | false |
| 1 | true |
| 2 | true |
Your turn
Implement the booking rule in a second language and run the unchanged shared cases.
Common mistake Changing the expected behavior to fit the second implementation.
Check your work
Both implementations pass. One sentence distinguishes what changed in the code from what stayed the same in the rule.
This browser could not save your progress. Changes last until you leave the page.