Skip to content

Lab · JavaScript Programming (language in depth)

Lab: a family train ticket

Labbeginner· about 1.5 hours· 5 steps

The brief

The Kulkarni family is taking the train from Pune to Delhi. Write the code behind their e-ticket: a passenger line with a template literal, the berth type from a berth number, a fare that depends on age, the total for the family, and the chart status summary.

Why it matters

Booking apps are full of this kind of code: formatting text for people, turning codes into words with lookup tables, applying rules with if / else, and counting with loops. It is also the bread and butter of JavaScript interview warm-up questions.

Skills you practise

  • template literals
  • trim and toUpperCase
  • the remainder operator and lookup tables
  • if / else if / else
  • for…of loops
  • counting with a loop
  • startsWith

What you will have at the end

ticket.js: five tested functions that print a family's train ticket and chart status.

How it is marked (rubric, 100 points)
Correct outputEvery check passes.50
Right tool for each jobTemplate literals for text, a lookup table for berths, else if in the right order, loops that reuse functions.25
Clean codeGood names, no copied rules.15
Can explainYou can explain why berth % 8 gives the position in the bay.10

0/5 steps done

Step 1 of 5

A passenger line

ticketLine(name, age, coach, berth) returns "ASHA KULKARNI (34) | S5/32": the name trimmed and in capitals, the age in brackets, then coach/berth.

A template literal is text in backticks where ${…} puts in the value of any expression. It is easier to read than joining many pieces with +. Clean the name first: trim() removes spaces typed by mistake at the ends, and toUpperCase() matches how e-tickets print names.

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