Run Your First Function in JavaScript
Scenario
Save the example as booking.mjs and run node booking.mjs.
// 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 assert.equal compares for the two inputs.
| Case | Result |
|---|---|
| canBook(1) | Select a choice above to see what happens. |
| canBook(0) | Select a choice above to see what happens. |
Reveal the explanation
- canBook(1)
The actual value true matches the expected value true.
- canBook(0)
The actual value false matches the expected value false.
Both assertions must pass before the terminal prints OK. Change the zero expectation to true to see a failure.
Shared booking contract
Accept a booking when at least one seat is free.
| Free seats | Expected |
|---|---|
| 0 | false |
| 1 | true |
| 2 | true |
Download shared test cases
Your turn
Run the JavaScript contract and explain the type of the result for one free seat.
Common mistake Comparing a boolean output with the text “true.”
Check your work
Three checks pass, and the explanation identifies a boolean.
This browser could not save your progress. Changes last until you leave the page.