Porting the Front Desk Script
The gym’s check-in is moving to a JavaScript tablet app, so the front desk’s Python script moves too. It hands out wristbands, adds up bills, warns when the class is full, keeps wall messages and counts the day’s check-ins, and its check-in now joins those pieces, and takes the kind of pass and whether they hire shoes. Run for four climbers, it prints seven lines:
M-101 Ana Reyes: £18.00
M-102 Ben Okafor: £11.00
L-101 Mia Novak: £13.00
Learn to Boulder is full
L-102 Leo Park: £18.00
Cave closed for resetting
Checked in today: 4
In this lesson, you rebuilt it in JavaScript, up to the morning’s two lines.
- The port keeps the Python file’s order: the constants first, then the functions, and the calls
that start the work last. So every
constis set before a function reads it, as you saw when the price table came too late (in “Knowing When a Name Exists”, line 8 calledpriceFor()before line 10’sconst PRICESran). The day’s count is alet, because the check-in changes it. makeBandCounter()comes over whole. Its number lives in alet, and thenonlocalline has no place in the port: JavaScript has none, and the arrow’s+=updates thenumberits factory declared.bill()comes over whole too, with three dots where Python writes a star. A star stops a JavaScript file before it runs. There’s nosum()built into JavaScript, so.reduce()adds the charges, starting from0.makeSession()takes the session’s name, its wristband prefix and its places. The warning callback comes in an options object, with a do-nothing arrow as its default: Python defaultson_fulltoNoneand checks for it, but the port’s default is safe to call. Only the class passes a warning, so= {}lets the morning’s call leave the options out. Each session keeps its own climbers and makes its own band counter.- The check-in arrow takes the climber, the kind of pass and options, where
shoesdefaults tofalse, as in Python. It pushes the climber, then adds one to the day’s count with noglobal: an assignment updates the nearest declaredcheckedInToday, theletat the top of the file. - Here the pieces meet. The bill adds the day pass to the shoe hire, or to
0. JavaScript’s conditional operator,shoes ? SHOE_HIRE : 0, does the job of Python’sif-elseexpression, with the test first. The line starts with the session’s next wristband, and.toFixed(2)writes the amount with two decimals, as Python’s:.2fdoes. - The desk’s lines come last. JavaScript has no keyword arguments, so Ana’s shoe hire travels in an
options object,
{ shoes: true }. Ben brings his own shoes, so his call passes none.