Introduction :
The Tip Calculator comprises three main components:
- HTML (HyperText Markup Language): Defines the structure and content of the web page.
- CSS (Cascading Style Sheets): Provides styling to enhance the visual presentation.
- JavaScript: Adds interactivity and logic to perform calculations.
Structure
HTML
The HTML file outlines the skeleton of the Tip Calculator. It includes a form where users can input their bill amount and desired tip percentage, and a button to trigger the calculation. The calculated total is then displayed on the same page.
- The
<head>
section includes meta tags for character encoding, compatibility, and responsiveness, and links the CSS file for styling. - The
<body>
contains a<div>
with a class ofcontainer
which holds all the interactive elements including:- An
<h1>
for the title. - Paragraph
<p>
for instructions. - Labels and input fields for entering the bill amount and tip percentage.
- A button to trigger the calculation.
- A span element to display the total amount.
- An
CSS
The CSS file styles the calculator to make it visually appealing and user-friendly.
- The
body
is styled with a light background and a specific font. - The
.container
class gives a centered, card-like appearance with padding, a shadow effect, and rounded corners. - Inputs and buttons are styled for uniformity, with padding, borders, and hover effects to enhance user interaction.
- The total span is emphasized with larger, bold text.
JavaScript Logic
The JavaScript file (app.js
) provides the core functionality of the Tip Calculator, handling user inputs and performing the necessary calculations.
Variable Declarations:
btnEl
: References the “Calculate” button.billInput
: References the input field for the bill amount.tipInput
: References the input field for the tip percentage.totalSpan
: References the span element where the total amount is displayed.
Function
calculateTotal
:- Retrieves the values entered by the user for the bill amount and tip percentage.
- Computes the total amount by adding the calculated tip to the bill amount. The formula used is
billValue * (1 + tipValue / 100)
. - Updates the
totalSpan
with the calculated total, formatted to two decimal places.
Event Listener:
- Adds a click event listener to the “Calculate” button, which triggers the
calculateTotal
function when the button is clicked.
- Adds a click event listener to the “Calculate” button, which triggers the
Purpose of Functions
calculateTotal
Function:- This function is crucial for the application’s functionality. It processes the user input, performs the arithmetic required to calculate the total amount including the tip, and updates the user interface with the result.
- It ensures that the total is displayed in a user-friendly format with two decimal places, making the application both practical and easy to use.
By combining these components, the Tip Calculator provides a seamless and interactive experience for users needing to quickly compute the total amount including a tip based on their bill. This project serves as an excellent example of integrating HTML, CSS, and JavaScript to build a functional web application.
SOURCE CODE:
HTML (index.html)
Tip Calculator
Tip Calculator
Enter the bill amount and tip percentage to calculate the total.
CSS (style.css)
* {
box-sizing: border-box;
}
body {
background-color: #f2f2f2;
font-family: "Helvetica", sans-serif;
}
.container {
background-color: white;
max-width: 600px;
margin: 100px auto;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
border-radius: 10px;
}
h1 {
margin-top: 0;
text-align: center;
}
input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin: 10px 0;
width: 100%;
}
button {
background-color: #6c7bb4;
color: white;
padding: 10px;
border: none;
cursor: pointer;
margin: 10px 0;
width: 100%;
font-size: 18px;
text-transform: uppercase;
transition: background-color 0.2s ease;
}
button:hover {
background-color: #45a049;
}
#total {
font-size: 24px;
font-weight: bold;
margin-top: 10px;
}
JavaScript (app.js)
const btnEl = document.getElementById("calculate");
const billInput = document.getElementById("bill");
const tipInput = document.getElementById("tip");
const totalSpan = document.getElementById("total");
function calculateTotal() {
const billValue = billInput.value;
const tipValue = tipInput.value;
const totalValue = billValue * (1 + tipValue / 100);
totalSpan.innerText = totalValue.toFixed(2);
}
btnEl.addEventListener("click", calculateTotal);