Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Porting the Front Desk Script

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:

Language: Program Output
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 const is set before a function reads it, as you saw when the price table came too late (in “Knowing When a Name Exists”, line 8 called priceFor() before line 10’s const PRICES ran). The day’s count is a let, because the check-in changes it.
  • makeBandCounter() comes over whole. Its number lives in a let, and the nonlocal line has no place in the port: JavaScript has none, and the arrow’s += updates the number its 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 no sum() built into JavaScript, so .reduce() adds the charges, starting from 0.
  • 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 defaults on_full to None and 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 shoes defaults to false, as in Python. It pushes the climber, then adds one to the day’s count with no global: an assignment updates the nearest declared checkedInToday, the let at 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’s if-else expression, 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 :.2f does.
  • 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The gym’s check-in is moving to a JavaScript tablet app, so the 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.

00:14 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. You’ll rebuild it in JavaScript, up to the morning’s two lines.

00:32 The port keeps the Python file’s order: the constants first, then the functions, and the calls last. So every constant is set before a function reads it, as you saw when the price table came too late.

00:46 The day’s count is a let, because the check-in changes it.

00:52 makeBandCounter() ports whole: you’ve written it before. Its number lives in a let, and there’s no nonlocal line: as when you first numbered the wristbands, the arrow’s += updates the number its factory declared.

01:08 The bill comes over whole too, with ... where Python writes a *. A * stops a JavaScript file before it runs, as you read when you first ported the bill.

01:19 There’s no sum() built into JavaScript, so .reduce() adds the charges, starting from zero.

01:27 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.

01:38 Python defaults to None and checks for it. The port’s default is safe to call. Only the class passes a warning, so the empty object lets the morning’s call leave the options out.

01:52 Each session keeps its own climbers, and makes its own band counter.

01:58 The check-in arrow takes the climber, the kind of pass, and options, where shoes defaults to false, as in Python. It pushes the climber and adds one to the day’s count.

02:10 There’s no global: as when you first moved the day’s count, the assignment updates the nearest checkedInToday it finds, the let at the top of the file.

02:19 The warning when the class fills is the exercise’s.

02:26 Here the pieces meet. The bill adds the day pass to the shoe hire, or to zero. JavaScript’s conditional operator does the job of Python’s if-else expression, with the test first.

02:40 The line starts with the session’s next wristband. .toFixed(2) writes the amount with two decimals, as Python’s format does.

02:50 The desk’s lines come last. The morning session gets the prefix M. JavaScript has no keyword arguments, so Ana’s shoe hire travels in an options object. Ben brings his own shoes, so his call passes none.

03:08 The class, the wall messages and the day’s count are the exercise’s.

03:14 Run the port. It logs Ana’s line first. Ben passes no options object at all, and the arrow pulls shoes out of it. What does Ben’s call log? Your turn: pause the video and predict.

03:35 It runs: wristband M-102, Ben Okafor, eleven pounds.

03:43 The call passes nothing for the options, so the parameter’s own default, the empty object, covers it. Then shoes falls back to false, its own default.

03:54 The bill adds eleven and zero. And the band is the morning counter’s second. Run the Python script, and it prints seven lines. The first two match the port’s, byte for byte.

04:09 That’s the desk’s morning, the JavaScript way: constants first, closures for the counters, rest and .reduce() for the bill, and options with defaults.

04:21 Next up is the exercise Finish the Front Desk Script. You’ll add the Learn to Boulder class and its full-class warning, then a message for each wall and the day’s count, so the JavaScript prints all seven lines.

Become a Member to join the conversation.