Sunday, September 24, 2023
Hire us on Freelancer.com
HomeJavascriptHow to make calculator in JavaScript

How to make calculator in JavaScript

In the field of web development, JavaScript is a powerful tool that empowers developers to create interactive and dynamic web applications. One such application is a calculator, which can be an essential addition to various websites. In this tutorial, we will walk you through the process of How can we make a calculator in JavaScript. By the end of this guide, you’ll have a functional calculator that you can integrate into your website or project.

Why Build a Calculator in JavaScript?

A calculator is a fundamental tool that customers frequently look for while browsing websites that need number operations. You may create a smooth and convenient experience for your visitors by embedding a calculator inside your website, giving them the hassle of traveling away to conduct calculations.

What Tools Do You Need?

To build a calculator, you’ll need a code editor of your choice (Visual Studio Code, Sublime Text, etc.) and a modern web browser to test your application.

How to make calculator in JavaScript:

Here’s a step-by-step breakdown of the process:

    1. HTML Structure: Begin by creating the HTML structure for your calculator. Use <div> elements to organize different parts of the calculator, such as display area and buttons.
    2. CSS Styling: Apply CSS styles to format and visually enhance your calculator. You can create buttons with rounded corners, a gradient background, and font icons to resemble a real calculator.
    3. JavaScript Logic: This is where the magic happens. Write JavaScript code to handle user interactions and perform calculations. Attach click event listeners to the buttons, capture user input, and update the display accordingly.
    4. Basic Operations: Implement basic arithmetic operations such as addition, subtraction, multiplication, and division. Use functions to perform these calculations and display the results on the screen.
    5. Clear and Equal Functions: Create functions to clear the display and calculate the final result when the user presses the “equals” button.
    6. Advanced Features (Optional): Depending on your requirements, you can add advanced features like decimal point support, percentage calculations, and more.

Throughout this tutorial, we’ll refer to specific resources to provide you with additional information and context. If you’re interested in deepening your understanding, you can refer to the following articles:

Source Code:

<!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">

    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css
">
    <title>Calculator in javascript</title>
    

    <style type="text/css">
        
        * {
            margin: 0;
            padding: 0;
        }

        *::after,
        *::before {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }



.cal-header-bg

    {
        height: 150px;
        width: 150px;
        border-radius: 50%;
        background-color: whitesmoke;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
    }

.cal-header-bg>span 

    {
        font-weight: 600;
        font-family: sans-serif;
        font-size: 1rem;
    }

.cal-header-bg>#heading 

    {
        color: red;
        
        text-align: right;
        margin-left: 5rem;

    }

.cal-header-bg>#inner-span 

    {
        margin-left: 8rem;
    }

.calculator 

    {
       
         border: 1px solid #eee;
        box-shadow: 0px 0px 15px rgb(0 0 0 / 10%);
    }



input[type=text] 

    {
        height: 100px;
        background-color: #37AB75;
        color: white;
        border: none;
        width: 100%;
        border-top-left-radius: 10px;
        border-top-right-radius: 10px;
        font-size: 25px;
        text-align: center;

    }

input::placeholder 

    {
        color: white;
        font-size: larger;
        padding: 1rem;
    }

input[type=text]:focus 

    {
        outline: none;
    }


.for-all-buttons 

    {
        display: grid;
        grid-template-columns: repeat(4, auto);
        gap: .5rem;
        background-color: white;
        padding: 40px 20px 40px 20px;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 10px;
        justify-content: space-between;
        align-items: center;
        justify-items: center;
    }

.btn 

    {
        border: none;
        background-color: #c0c0c05e;
        border-radius: 50%;
        height: 45px;
        width: 45px;
        font-size: 1.5rem;
        font-weight: 700;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
       
    }

#equal 

    {
        background-color: rgb(16, 224, 224);
        color: #fff;
    }

#plus 

    {
        background-color: rgb(46 221 75 / 77%);
        color: #fff;
    }

#minus 

    {
        background-color: rgb(152 76 255 / 79%);
        color: #fff;
    }

#multi 


    {
        background-color: rgb(184 70 141 / 73%);
        color: #fff;
    }

#del 

    {
        font-size: 18px;
        border: 1px solid rgb(224, 127, 127);

    }

#clear 

    {
        border: none;
        background-color: red;
        color: white;
        border-radius: 50%;
        height: 45px;
        width: 45px;
        font-size: 1.5rem;
        font-weight: 700;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
    }



    </style>

</head>

<body>

    <div class="container mt-5">

       <div class="row d-flex justify-content-center">
           
           <div class="col-xxl-4 col-lg-4 col-md-6 col-sm-12 col-7 text-center mb-5">

                <div class="cal-box">

                    <div class="mb-3">
                        <div class="cal-header-bg">
                          
                            <h1 id="heading">Javascript</h1>
                            <span id="inner-span">Calculator</span>
                        </div>
                    </div>
               
                    <div class="calculator">
                        <form>
                            <input type="text" placeholder="0" id="input">
                        </form>

                        <div class="for-all-buttons">

                            <button onclick="calc(7)" class="btn">7</button>
                            <button onclick="calc(8)" class="btn">8</button>
                            <button onclick="calc(9)" class="btn"> 9</button>
                            <button onclick="calc('+')" class="btn" id="plus">+</button>
                            <button onclick="calc(4)" class="btn">4</button>
                            <button onclick="calc(5)" class="btn">5</button>
                            <button onclick="calc(6)" class="btn">6</button>
                            <button onclick="calc('-')" class="btn" id="minus">-</button>
                            <button onclick="calc(1)" class="btn">1</button>
                            <button onclick="calc(2)" class="btn">2</button>
                            <button onclick="calc(3)" class="btn">3</button>
                            <button onclick="calc('*')" class="btn" id="multi">X</button>
                            <button onclick="calc(0)" class="btn">0</button>
                            <button onclick="calc('.')" class="btn">.</button>
                            <button onclick="calc('/')" class="btn">/</button>
                            <button onclick="eqaul('=')" class="btn" id="equal"> = </button>
                            <button class="btn" onclick="Clear()" id="clear"> C </button>
                            <button class="btn" onclick="del()" id="del"> Del </button>

                        </div>

                    </div>
                    
                </div>

           </div>

       </div>
        
    </div>





    <script type="text/javascript">
        
        let input = document.getElementById('input')

        function calc(param) 

        {
            input.value += param;
        }

        function del() 

        {
            input.value = input.value.slice(0, -1);
        }

        function Clear() 

        {
            input.value = '';
        }

        function eqaul()

        {
            input.value = eval(input.value)
        }

    </script>


</body>

</html>

For more in-depth tutorials on web development and JavaScript projects, visit developerstricks.com. This resource hub offers articles that cover a wide range of topics, helping you enhance your skills and stay updated with the latest trends. Happy coding!

 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Hire Us

Categories