Skip to content

Lab · JavaScript Programming (language in depth)

Lab: tokens and fees at a clinic

Labbeginner· about 1.5 hours· 5 steps

The brief

Arogya Clinic in Lucknow hands out paper tokens and works out fees in a notebook. Write the functions for its new app: token numbers with default parameters, a token machine that remembers its count (a closure), fees from an options object, a send-only-once guard, and a patient count across nested departments with recursion.

Why it matters

Closures, default and rest parameters, options objects and higher-order functions are everywhere in real JavaScript: React hooks, event handlers, Express middleware and utility libraries are built from them. They also come up in nearly every JavaScript interview.

Skills you practise

  • default parameters
  • padStart
  • closures that keep state
  • options objects with destructuring
  • rest parameters
  • higher-order functions
  • recursion

What you will have at the end

clinic.js: tested functions for tokens, fees, a send-once guard and patient counts.

How it is marked (rubric, 100 points)
Correct resultsEvery check passes.50
Uses the right featureDefaults, a closure for the counter, an options object, rest parameters, a wrapper function and recursion.25
Clean codeSmall functions with clear names; no global counters.15
Can explainYou can explain what a closure remembers and why each machine counts on its own.10

0/5 steps done

Step 1 of 5

Token numbers

formatToken(number, prefix) returns "OPD-007" style text: the prefix (OPD when not given), a dash, and the number padded to 3 digits.

A default parameter (prefix = 'OPD') is used when the caller leaves that argument out, so most calls stay short. padStart(3, '0') adds zeros at the front until the text is 3 characters long, which keeps tokens the same width on the display board. It works on text, so turn the number into a string first.

Run shows what your code prints. Check runs the step's tests. Ctrl+Enter checks too.