Tip Calculator using HTML, CSS and JavaScript – Coding Torque

Share your love

Hello Guys! In this blog, I’m going to explain to you how to make a Tip Calculator using HTML, CSS, and JavaScript. You can use this project and upgrade it as a huge app by adding more calculation functionalities to it. This will be a step-by-step guide. Let’s get started 🚀.

Before we start, here are some JavaScript Games you might like to create:

1. Snake Game using JavaScript

2. 2D Bouncing Ball Game using JavaScript

3. Rock Paper Scissor Game using JavaScript

4. Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript

 

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

 

HTML Code 

<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />

    <!-- Google Fonts  -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">

    <title>Tip Calculator using JavaScript - @code.scientist x @codingtorque</title>
</head>

<body>
    <div class="calculator">
        <label for="totalBill">Bill</label>
        <input type="number" oninput="calculateTip()" onchange="calculateTip()" id="totalBill"
            placeholder="Enter Your Bill">
        <label for="tipPercentage" style="display: flex; justify-content:space-between;">
            <span>Tip %</span>
            <span id="tipPercentageText">15%</span>
        </label>
        <input type="range" oninput="calculateTip()" onchange="calculateTip()" id="tipPercentage">
        <label for="numOfPerson" style="display: flex; justify-content:space-between;">
            <span>
                No. of people
            </span>
            <span id="numOfPersonText">15</span>
        </label>
        <input type="range" oninput="calculateTip()" onchange="calculateTip()" id="numOfPerson">
        <div class="result" id="result">
            <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                <span>Tip</span>
                <span style="font-size:1.2rem;">__.__</spam>
            </h5>
            <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                <span>Total</span>
                <span style="font-size:1.2rem;">__.__</spam>
            </h5>
            <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
                <span>Each Person Pay</span>
                <span style="font-size:1.2rem;">__.__</spam>
            </h5>
        </div>
    </div>
</body>

</html>

Output Till Now

CSS Code 

Create a file style.css and paste the code below.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: "Poppins", sans-serif;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #4e4cff;
}

.calculator {
    margin-top: 7rem;
    display: flex;
    flex-direction: column;
    background-color: white;
    border-radius: 15px;
    height: 30rem;
    width: 20rem;
    padding: 20px 30px;
}

#totalBill {
    margin: 10px 0;
    height: 38px;
    border: none;
    background: #f1f1f1;
    padding: 0 10px;
    border-radius: 5px;
}

#totalBill:focus {
    outline: 2px solid #4b49f1;
}

input[type='range'] {
    accent-color: #4b49f1;
    margin: 10px 0;
}

.result {
    width: 100%;
    min-height: 100px;
    margin-top: 20px;
    padding: 20px;
    background-color: #4b49f1;
    border-radius: 15px;
    font-size: 1.2rem;
    color: white;
}

Output Till Now

tip calculator using javascript with source code

JavaScript Code 

Create a file script.js and paste the code below.
let result = document.getElementById("result");

const calculateTip = () => {
    let bill = Number(document.getElementById("totalBill").value);
    let numOfPerson = Number(document.getElementById("numOfPerson").value);
    let tipPercentage = Number(document.getElementById("tipPercentage").value);

    let tip = bill * (tipPercentage / 100);
    let totalBill = bill + tip;
    let perPersonPay = (totalBill / numOfPerson);

    document.getElementById("tipPercentageText").innerHTML = `${tipPercentage}%`;
    document.getElementById("numOfPersonText").innerHTML = `${numOfPerson}`;

    result.innerHTML = `            
        <h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Tip</span>
    <span style="font-size:1.2rem;">${tip.toFixed(2)}</span>
</h5>
<h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Total</span>
    <span style="font-size:1.2rem;">${totalBill.toFixed(2)}</span>
</h5>
<h5 style="margin: 8px 0;display: flex; justify-content:space-between;">
    <span>Each Person Pay</span>
    <span style="font-size:1.2rem;">${perPersonPay.toFixed(2)}</span>
</h5>
    `
}
Written by: Piyush Patil
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Share your love