JavaScript AcademyProfessional Engineering

Level 1 · Foundations Project 01 of 60 Runs in the browser

Project 1: Interactive Tip Calculator

A tip calculator that splits a bill fairly: you write and test the logic, the page is provided.

Level 1 covers values, decisions and functions, so you write tip.js. The page code (app.js) is provided and explained; you will write that kind of code yourself in Level 3.

1 · The problem

Friends splitting a restaurant bill want the tip, the total and each person's share, and nobody wants to pay a cent too little or too much. Typing mistakes such as an empty field or 2.5 people must be caught with a clear message.

User stories

  • As a diner, I want to enter the bill, tip and number of people and see what each person pays.
  • As a diner, I want to be told which field is wrong if I make a typing mistake.
  • As a developer, I want the calculations covered by tests so I can change them safely.

2 · Requirements

  • Calculate the tip for a bill and a tip percentage, rounded to cents.
  • Show the bill plus tip as the total.
  • Split the total between 1 and 50 people, rounding each share up to the next cent.
  • Reject an empty, non-numeric, zero or negative bill, and bills over 100,000.
  • Reject tips outside 0–100% and people counts that are not whole numbers from 1 to 50.
  • Show every error next to its field, announced to screen readers.
  • Keep the logic in pure functions that can be tested without the page.

Acceptance criteria

  • Bill 80, tip 15%, 2 people shows Tip $12.00, Total $92.00, Per person $46.00.
  • Bill 100, tip 0%, 3 people shows $33.34 per person.
  • Bill 'ten' shows 'Enter a bill amount greater than 0.' next to the bill field.

Architecture

LayerFileResponsibility
Pageindex.html + style.cssForm with labelled inputs, error messages and results.
Logictip.jsPure functions: calculateTip, totalWithTip, splitAmount, validateInputs. Your work.
Wiringapp.jsReads the form on submit, calls validateInputs, shows errors or results. Provided.

Flow: Submit → validateInputs(text) → errors? show them : calculateTip / totalWithTip / splitAmount → format and display

Expected result

Interactive Tip Calculator: finished app
finished app

Screenshots are captured automatically from the solution by the browser test suite.

Build it: guided stages

Pick a stage, load its starter files, follow the steps, then run the app and the tests. Every stage is preserved separately, so loading one never overwrites another.

  1. Stage 1: Calculate the tip · Write calculateTip so the app can show the tip for a bill.
  2. Stage 2: Total and split · Add the total and each person's share, rounding the share up to the next cent.
  3. Stage 3: Validate and finish · Reject impossible input with clear messages, then test the whole app.

Troubleshooting

The result shows $NaN.
A function returned undefined or a value is text. Check every function has a return statement; run the logic tests to see which one.
Per person is one cent too high for some totals.
total * 100 can be slightly above a whole number (floating point). Round it with Math.round before Math.ceil.
Calculate shows nothing and no error.
Open the console panel: an error in tip.js stops app.js too. Fix the first error reported.

Extension challenges

  1. Add tip buttons for 10%, 15% and 20% that fill the tip field.
  2. Show how much each person tips as well as what they pay in total.
  3. Round each share to the nearest 5 cents for cash, and add tests for the new rounding rule.

Downloads

Browser projects open directly from index.html. Node.js unit tests: run node --test in the project folder.

AI development prompts

Context: I am completing the academy's Tip Calculator, stage 2. Task: explain why splitAmount(89.1, 1) can return 89.11 without Math.round. Constraints: explain floating point in plain language; do not rewrite my function. Expected behaviour: I can explain the bug and fix it myself. Testing: suggest two more totals that would expose it.
Context: My validateInputs function passes the visible tests. Task: list boundary inputs I should test for bill, tip and people. Constraints: do not write the implementation. Expected behaviour: every rule is tested just inside and just outside its limit. Testing: give each case as input and expected ok value.
Context: I want to add preset tip buttons. Task: help me plan the change. Constraints: keep tip.js free of page code; existing tests must pass. Expected behaviour: a button fills the tip field and recalculates. Testing: describe an acceptance test for one button.

Source

Repository folder: projects/project-01-interactive-tip-calculator/ · view on GitHub (repository pending publication)