Skip to content

Tip Calculator

Easy~20 min

What you'll build

A page with two input fields (bill, tip %) and a result area. As the user types, the tip amount and total update live.

Goals

  • Read and parse numeric input from form fields
  • Write a pure function that computes tip and total
  • Update the DOM on every input event
  • Format a number as currency with toFixed

Prerequisites

  • Functions and return values
  • Number parsing (Number, parseFloat)
  • DOM selection and event listeners

Steps

  1. 1

    Write the HTML

    Create two number inputs and a result span. Labels make the inputs clear; the span is where the result will appear.

    Code
    <label>Bill amount <input type="number" id="bill" value="20"></label>
    <label>Tip % <input type="number" id="tip" value="15"></label>
    <p id="result">Tip: $0.00 | Total: $0.00</p>

    Expected result

    Two labeled number inputs and a result paragraph appear.

  2. 2

    Write the calculation function

    Write calcTip(bill, percent) that returns an object with tip and total. Keeping the math in a pure function makes it easy to test and reuse.

    Code
    function calcTip(bill, percent) {
      const tip = bill * percent / 100;
      return { tip, total: bill + tip };
    }

    Expected result

    calcTip(20, 15) returns { tip: 3, total: 23 }.

  3. 3

    Read inputs and update the DOM

    Select the inputs and result span. Write an update() function that reads the values, calls calcTip, and sets the result text. Use toFixed(2) to always show two decimals.

    Code
    const billEl = document.getElementById("bill");
    const tipEl = document.getElementById("tip");
    const resultEl = document.getElementById("result");
    
    function update() {
      const bill = Number(billEl.value);
      const percent = Number(tipEl.value);
      const { tip, total } = calcTip(bill, percent);
      resultEl.textContent = `Tip: $${tip.toFixed(2)} | Total: $${total.toFixed(2)}`;
    }

    Expected result

    Calling update() shows the current tip and total based on the input values.

  4. 4

    Listen for input events

    Attach an input listener to both inputs that calls update(). The input event fires on every keystroke, so the result updates live as the user types.

    Code
    billEl.addEventListener("input", update);
    tipEl.addEventListener("input", update);
    update(); // run once on load

    Expected result

    The result updates immediately when either input changes, and on page load.

Try it yourself

What you learned

You built a live-updating calculator by combining a pure math function, input event listeners, and DOM updates — the pattern behind every interactive form widget.

Related Resources

Related Lessons

  • Functions

    The pure calcTip function that holds the math.

  • DOM

    The input event listeners that trigger live updates.

Related Practice

Related Tools

  • Math Evaluator

    Compare your tip calculation with a full expression evaluator.

Related Cheatsheets

Related Reference

Related Errors

Related Glossary

  • Function

    Reusable code blocks.

  • Variable

    Variable assignment and state.

  • DOM

    Document Object Model basics.

Related Glossary

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

  • Function

    Reusable code blocks.

  • Variable

    Variable assignment and state.

  • DOM

    Document Object Model basics.

Related Errors

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

← Back to topic