Build a Percentage Calculator using JavaScript

Build a Winning Percentage Calculator using JavaScript

A percentage calculator is a tool or formula used to simplify and automate the process of calculating percentages. It is a valuable tool in various fields, including finance, education, business, and everyday calculations.

Introduction

In the vast landscape of web development, incorporating interactive tools into your website is crucial for user engagement, one such valuable feature is a Percentage Calculator, providing users with a quick and efficient way to compute percentages. In this comprehensive guide, we will walk through the process of creating a percentage calculator using HTML, CSS, and JavaScript. With this tutorial, you will gain insight into the science of the calculator.

How Percentage is Calculated

The percentage calculator typically performs calculations involving three main values: the part, the whole, and the percentage.

Percentage Calculator Formula

In this formula:

  • Percentage: The percentage value is being calculated.
  • Part: The portion or fraction of the whole that you are interested in.
  • Whole: The total or complete quantity.

Learn more about the percentage.

The Significance of a Percentage Calculator

A percentage calculator is more than just a functional tool; it adds practicality to your website. Whether you’re running a blog, an e-commerce platform, or a business site, integrating tools that simplify calculations can significantly enhance the user experience.

Winning Percentage Calculator

Percentage calculators can be used for various purposes, such as calculating discounts, determining percentage change, finding percentages of a given quantity, and much more. They are often available as standalone tools, features in calculators, or built-in functions in spreadsheet software, providing convenience in performing percentage-related calculations accurately and efficiently.

Step 1: Establishing the HTML Structure

Begin by setting up the HTML structure for your own percentage calculator. The code includes input fields for the total value and percentage, a button to trigger the calculation, and a paragraph to display the result.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Percentage Calculator</title>
</head>
<body>
    <div class="container">
        <h1>Percentage Calculator</h1>
        <label for="total">Total Value:</label>
        <input type="number" id="total">

        <br/>

        <label for="percentage">Percentage:</label>
        <input type="number" id="percentage">
        
        <br/>

        <button onclick="calculatePercentage()">Calculate</button>

        <hr />

        <h4 id="result">Result: </h4>
    </div>
    <script src="script.js"></script>
</body>
</html>

This sets up the basic structure with input fields for the total value and percentage, a button to trigger the calculation, and a paragraph to display the result.

Step 2: Enhancing Design with CSS

A visually appealing design is crucial for user engagement. Create a separate CSS file to style your percentage calculator. This not only improves aesthetics but also ensures a consistent and professional appearance.

body {
    font-family: 'Arial', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    font-size: 17px;
    background: #fafafa;
}
h1 {
    margin: 5px 0 15px 0;
    text-align: center;
    text-transform: uppercase;
    font-size: 20px;
    letter-spacing: 1px
}
.calculator {
    width: 400px;
    padding: 20px;
    border: 1px solid #ccc;
    border-radius: 5px;
    background: #fff;
}
label, input, button {
    margin: 10px 0;
}
input {
    display: block;
    width: calc(100% - 20px);
    padding: 10px;
}
button {
    background-color: #000;
    color: #fff;
    outline: 0;
    border: 0;
    padding: 10px 15px;
    cursor: pointer;
    font-size: 16px;
    margin: 0 auto; 
    border-radius: 5px;
    margin-bottom: 5px;
}
hr {
    margin: 15px 0;
}
h4 {
    margin-bottom: 0;
}

These styles create a clean and modern design for your percentage calculator.

Step 3: Adding Functionality with JavaScript

The heart of the percentage calculator lies in its functionality, driven by JavaScript. The script takes user input, validates it, performs the percentage calculation, and updates the HTML to display the result.

// JavaScript Functionality
function calculatePercentage() {
    const total = parseFloat(document.getElementById('total').value);
    const percentage = parseFloat(document.getElementById('percentage').value);
    
    if (isNaN(total) || isNaN(percentage)) {
        alert("Please enter valid numbers.");
        return;
    }

    const result = (total * percentage) / 100;
    document.getElementById('result').innerHTML = `Result: ${percentage}% of ${total} is ${result.toFixed(2)}`;
}

This JavaScript function calculates the percentage based on the entered total value and percentage and displays the result.

Conclusion

Congratulations! You’ve successfully built a percentage calculator using HTML, CSS, and JavaScript. Remember to test thoroughly, customize the calculator to suit your specific needs, and continue exploring ways to integrate valuable features into your web projects. Happy coding!

Related Posts