You've just finished a great dinner with friends. The bill arrives, and now comes the hard part: figuring out the tip and how much each person owes. Doing percentage math in our heads after a big meal isn't much fun, so let's build a tip calculator that does the work for us. Play with the one below (or open it in a new window):
Your tip calculator needs to do the following:
For extra credit, add a field where people can enter their own custom tip percentage. Also, make sure your calculator doesn't fall apart when the bill field is empty or contains something that isn't a number.
Onwards!
Create a new HTML document in your favorite code editor (like Visual Studio Code) and copy/paste the following starting content into it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tip Calculator</title>
<style>
</style>
</head>
<body>
<h1>Tip Calculator</h1>
<label for="bill">Bill</label>
<input id="bill" type="number" min="0" step="0.01" placeholder="0.00">
<p>Tip</p>
<div class="tips">
<button data-tip="10">10%</button>
<button data-tip="15">15%</button>
<button data-tip="18">18%</button>
<button data-tip="20">20%</button>
<button data-tip="25">25%</button>
</div>
<label for="people">People</label>
<input id="people" type="number" min="1" value="1">
<p>Tip per person: <span id="tipPerPerson">$0.00</span></p>
<p>Total per person: <span id="totalPerPerson">$0.00</span></p>
<script>
</script>
</body>
</html>
This gives us inputs for the bill and the number of people, buttons for picking a tip, and spots for displaying the results. Keep in mind that the value we get from an input element is always a string, even when its type is set to number. The styling and the JavaScript for doing all of the math are what you will add entirely on your own!
The following tutorials may provide some helpful tips and techniques to help you with this exercise: Events in JavaScript, Going from a String to a Number, Rounding Numbers in JavaScript, Custom Data (data-*) Attributes, Handling Events for Many Elements
Once you have completed this challenge, you have earned the awesome bragworthy privilege of adding the following badge to your collection:

To claim it, head over to the forums and respond in the Tip Calculator topic. Be sure to include a link to your solution or insert a copy of your HTML/CSS/JS in the body of the message:

Once you have created your topic, Kirupa will give you a virtual high-five and ensure this badge is added to your list of assigned badges.
We want to make this a fun learning activity. If you are stuck and need help, please ask on the forums. Please explain your problem in detail and one of the many helpful forum members will help you out.
Similarly, if you want feedback on your code, please also ask in the forums. This would be an excellent chance for you to learn how you can improve your solution from other web developers!
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

:: Copyright KIRUPA 2026 //--