Tip Calculator
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
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
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.Codefunction calcTip(bill, percent) { const tip = bill * percent / 100; return { tip, total: bill + tip }; }Expected result
calcTip(20, 15) returns { tip: 3, total: 23 }.
- 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.Codeconst 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.
Hint
Number("20") returns 20; Number("") returns 0, so empty inputs do not break the math.
- 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.
CodebillEl.addEventListener("input", update); tipEl.addEventListener("input", update); update(); // run once on loadExpected result
The result updates immediately when either input changes, and on page load.
Try it yourself
<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>
<script>
// TODO: calcTip, update, wire input listeners
</script><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>
<script>
function calcTip(bill, percent) {
const tip = bill * percent / 100;
return { tip, total: bill + tip };
}
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)}`;
}
billEl.addEventListener("input", update);
tipEl.addEventListener("input", update);
update();
</script>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
Related Practice
- Functions Concept
Solidify parameters, return values, and pure functions.
- Number Predict
Predict the result of Number() parsing and arithmetic.
Related Tools
- Math Evaluator
Compare your tip calculation with a full expression evaluator.
Related Cheatsheets
- JavaScript Cheatsheet
Function, Number, and DOM event syntax reference.
Related Reference
- JavaScript Math
Math object reference.
Related Errors
- NaN Result
Not-a-Number results from parsing.
- Type Error Not a Function
Calling a non-function value.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- NaN Result
Not-a-Number results from parsing.
- Type Error Not a Function
Calling a non-function value.