Home >Web Front-end >CSS Tutorial >Build a Tip Calculator Website

Build a Tip Calculator Website

PHPz
PHPzOriginal
2024-08-19 04:24:05739browse

Build a Tip Calculator Website

Introduction

Hello, fellow developers! I'm thrilled to introduce my latest project: a simple yet handy Tip Calculator. This project is a perfect opportunity to practice fundamental JavaScript concepts, such as handling user inputs, performing calculations, and dynamically updating the DOM. Whether you're new to JavaScript or looking for a small project to sharpen your skills, this Tip Calculator is a great choice.

Project Overview

The Tip Calculator is a web-based application that allows users to quickly calculate the total amount to pay, including the tip, based on the bill amount and tip percentage. The project demonstrates how to create an interactive user interface, manage calculations, and provide real-time feedback to the user.

Features

  • User-Friendly Interface: Simple and intuitive design for easy use.
  • Real-Time Calculation: Instantly calculates the total amount as you input the bill and tip percentage.
  • Responsive Design: The layout adjusts to different screen sizes, ensuring a seamless experience on both desktop and mobile devices.
  • Reset Functionality: Users can easily reset the input fields and start a new calculation.

Technologies Used

  • HTML: Structures the web page and input elements.
  • CSS: Styles the interface, ensuring a clean and responsive design.
  • JavaScript: Handles the calculation logic and updates the total amount in real-time.

Project Structure

Here's a quick look at the project structure:

Tip-Calculator/
├── index.html
├── styles.css
└── script.js
  • index.html: Contains the HTML structure for the Tip Calculator.
  • styles.css: Includes CSS styles to enhance the appearance of the calculator.
  • script.js: Manages the calculation logic and dynamic updates.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Tip-Calculator.git
    
  2. Open the project directory:

    cd Tip-Calculator
    
  3. Run the project:

    • Open the index.html file in a web browser to start using the Tip Calculator.

Usage

  1. Open the website in a web browser.
  2. Enter the bill amount in the designated input field.
  3. Enter the tip percentage to be added to the bill.
  4. Click the "Calculate" button to see the total amount including the tip.
  5. Reset the fields if you want to start a new calculation.

Code Explanation

HTML

The index.html file provides the basic structure of the Tip Calculator, including the input fields for the bill amount and tip percentage, as well as the button to trigger the calculation. Here’s a snippet:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923

93f0f5c25f18dab9d176bd4f6de5d30e
    1fc2df4564f5324148703df3b6ed50c1
    4f2fb0231f24e8aef524fc9bf9b9874f
    b2386ffb911b14667cb8f0f91ea547a7Tip Calculator6e916e0f7d1e588d4f442bf645aedb2f
    e4bb307b77bfd1a1ec7fd6c485d2bfb1
    5de102113aede4703971b3b780c58efb2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1

6c04bd5ca3fcae76e30b72ad730ca86d
    4883ec0eb33c31828b7c767c806e14c7
        4a249f0d628e2318394fd9b75b4636b1Tip Calculator473f0a7621bec819994bb5020d29372a
        e388a4556c0f65e1904146cc1a846beeEnter the bill amount and tip percentage to calculate the total amount94b3e26ee717c64999d7867364b1b4a3

        da55a23ba75f1a91499fd53db6dde99fBill amount:8c1ecd4bb896b2264e0711597d40766c
        da68c775d7dd560fd6878751c0c9e570
        0c6dc11e160d3b678d68754cc175188a

        54616200344afdb89867e9df0f118122Tip percentage:8c1ecd4bb896b2264e0711597d40766c
        2f56d4979a58a88631c556197730f8be
        0c6dc11e160d3b678d68754cc175188a

        c64b5582d11b3d494d9cfa5ac00d34dbCalculate65281c5ac262bf6d81768915a4a77ac0
        0c6dc11e160d3b678d68754cc175188a

        f42db1d3de5697d736e79e701ac6f5b5Total Amount:8c1ecd4bb896b2264e0711597d40766c
        2999737d3770e77603f9c3571ac488af54bdf357c58b8a65c66d7c19c8e4d114
    16b28748ea4df4d9c2150843fecfba68

    ffd6ba4147bda351239915f463e46e38
        e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
36cc49f0c466276486e50c850b7e4956

73a6ac4ed44ffec12cee46588e518a5e

CSS

The styles.css file styles the Tip Calculator, providing a clean and responsive layout. Here are some key styles:

* {
    box-sizing: border-box;
}

body {
    color: white;
    background-color: white;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.container {
    background-color: rgb(0, 0, 0);
    max-width: 600px;
    margin: 100px auto;
    padding: 20px;
    box-shadow: inset;
    border-radius: 10px;
}

h1 {
    text-align: center;
}

input {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    margin: 10px 0;
    width: 100%;
}

button {
    background-color: green;
    color: white;
    padding: 10px;
    border: none;
    cursor: pointer;
    margin: 10px 0;
    text-transform: uppercase;
    transition: background-color 0.2s ease;
}

button:hover {
    background-color: #45a049;
}

#total {
    font-size: 24px;
    font-weight: bold;
    margin-top: 10px;
}

.footer {
    margin: 70px;
    text-align: center;
    color: black;
}

JavaScript

The script.js file handles the calculation logic, updating the total amount based on user inputs. Here’s a snippet:

const btn = document.getElementById("calculate");
const inputBill = document.getElementById("bill");
const inputTip = document.getElementById("tip");
const totalSpan = document.getElementById("total");

function calculateTotal() {
    const billValue = parseFloat(inputBill.value);
    const tipValue = parseFloat(inputTip.value);
    const totalValue = billValue * (1 + tipValue / 100);
    totalSpan.innerText = totalValue.toFixed(2);
}

btn.addEventListener("click", calculateTotal);

Live Demo

You can check out the live demo of the Tip Calculator here.

Conclusion

Building this Tip Calculator was a fun and educational experience that reinforced my understanding of JavaScript, particularly in creating interactive web applications. I hope this project inspires you to experiment with JavaScript and build your own useful tools. Happy coding!

Credits

This project was developed as part of my ongoing journey to enhance my web development skills, with a focus on JavaScript and DOM manipulation.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Build a Tip Calculator Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn