Command Palette

Search for a command to run...

Hectal
Phase 3: Functions
JavaScript · Exercise 6 of 6 Previous
Intermediate

Build a bill splitter

Build a small app that splits a restaurant bill, with a pure core and an impure shell.

Pure core: calcSplit(bill, tipPercent, people) returns how much each person pays, including the tip, rounded to 2 decimals (as a number). If people is less than 1, it returns null. It must not touch the page.

Impure shell: when Split is clicked, read the three inputs, call calcSplit, and show the result in #result as ₹275.00 per person (two decimals). If calcSplit returns null, show Enter at least 1 person instead.

What Check tests

  1. calcSplit(1000, 10, 4) is 275
  2. The result is rounded to 2 decimals: calcSplit(100, 15, 3) is 38.33
  3. calcSplit returns null for fewer than 1 person
  4. calcSplit is pure: same answer twice, page untouched
  5. Clicking Split shows ₹275.00 per person
  6. With 0 people the page asks for at least 1 person

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.