Curriculum

Curriculum

Run Your First Function in JavaScript

Scenario

Save the example as booking.mjs and run node booking.mjs.

Selected choicePick a choice below
Result—
JavaScript
// 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.mjs

Try it yourself

Predict what assert.equal compares for the two inputs.

Reveal the explanation
  1. canBook(1)

    The actual value true matches the expected value true.

  2. 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.

Read free seats
Is the count greater than zero?
Yes
Accept
No
Decline
Free seatsExpected
0false
1true
2true
Runnable reference files
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.