CALCULATOR

Whether you're a new web developer or just looking to add a fun project to your portfolio, this step-by-step guide will provide you with the knowledge and skills you need to fulfill your Web development dreams. We'll start with the basics, setting up the HTML structure, and then move on to styling using CSS to make the Calculator look good. Finally, we'll take a closer look at JavaScript to implement Calculator logic to ensure a smooth and interactive.

This JavaScript project is a simple calculator application designed to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. It provides users with a convenient tool for performing mathematical calculations within a web browser.

Challenges Faced: During development, one of the main challenges was implementing the logic to handle different types of user input and ensuring accurate calculation results. Additionally, designing an intuitive and responsive user interface posed its own set of challenges, especially in terms of layout and styling.

Performance Optimization: Optimizing the performance of the calculator application, especially for complex calculations or large datasets, required strategic optimizations in algorithm design and code efficiency to ensure smooth user experience without significant latency.

Source Code

Step 1(HTML Code): To begin, create the basic structure of your Calculator using HTML. Define the flex where the Calculator will take place and set up the necessary elements.

  1. 1. <!DOCTYPE html>: This declaration defines the document type and version of HTML being used (HTML5 in this case).
  2. 2. <html lang="en">: The opening tag for the HTML document. The lang attribute is set to "en" for English, specifying the language of the document.
  3. 3. <head:> Contains meta-information about the HTML document, such as character set, viewport settings, and the page title.
    • <meta charset="UTF-8">: Sets the character encoding to UTF-8, which is a widely used character encoding for the web.
    • <meta name="viewport" content="width=device-width, initial-scale=1.0">: Configures the viewport settings for responsive design.
    • <title> Calculator </title>: Sets the title of the web page, which is displayed in the browser's title bar or tab.
    • Google Fonts are included to style the text on the page.
  4. 4. <body>: Contains the content of the HTML document.
  5. 5. <div id="container">:The main container that holds all the content.
    • <div class="calculator">: Contains information about the under this "calculator" class.
    • <div class="main-input">: This calculation is displayed in the class of "main-input".
    • <div class="button-row">: These are all the numbers and operators in the "button-row" class from which you can operate..
    • <script src="script.js"></script>: Links an external JavaScript file (script.js) to the HTML document, providing dynamic functionality and interactivity to the page.

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator Made By Me...</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="calculator">
<input type="text" id="main-input" placeholder="0">

 <div class="button-row mt-4">
    <button class="operator">AC</button>
    <button class="operator">DEL</button>
    <button class="operator">%</button>
    <button class="operator">/</button>
 </div>
   
 <div class="button-row mt-3">
     <button>7</button>>
     <button>8</button>>
     <button>9</button>>
     <button class="operator">*</button>>
  </div>

 <div class="button-row mt-3">
   <button>4</button>
   <button>5</button>
   <button>6</button>
   <button class="operator">-</button>
</div>

<div class="button-row mt-3">
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button class="operator">+</button>
</div>

<div class="button-row mt-3">
    <button>00</button>
    <button>0</button>
    <button>.</button>
    <button id="equals">=</button>
</div>

Step 2(CSS Code): Make your Calculator visually appealing by adding styles with CSS. Design the project board, pieces, and overall layout to enhance the user experience. let's break down the CSS code:

  1. 1. Body Styles:

    CSS

    body {
    background:linear-gradient(45deg, #0a0a0a, #3a4452);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    }
    • Sets the background color of the entire page to a gradient-Black.
    • height: 100vh; ensures that the container takes at least the full height of the viewport.
    • Set the display-flex to set the aligning verticaly and horizontaly of Calculator.
  2. 2. calculator:

    CSS

    .calculator{
    border: 1px solid #717377;
    padding: 20px;
    border-radius: 16px;
    background: transparent;
    box-shadow: 0px 3px 15px rgba(113, 115,119, 0.5);
    }
    • Has a background color, border, border radius, and padding.
  3. 3. main-input

    CSS

    #main-input {
    width: 320px;
    border: none;
    padding: 24px;
    margin: 10px;
    background-color: transparent;
    box-shadow: 0px 3px 15px rgbs(84, 84, 84, 0.1);
    font-size: 48px;
    text-align: right;
    cursor: pointer;
    color: #fff;
    outline: 0;
    } 
    
    • Set the width: 320px; for input calculation
    • Has a background, margin, font-size and padding.
  4. 4. Button:

    CSS

    button {
    border: 0;
    width: 60px;
    height: 60px;
    margin: 10px;
    border-radius: 50px;
    background: transparent;
    color: #ffffff;
    font-size: 20px;
    cursor: pointer;
    box-shadow: -8px -8px 15px rgba(255, 255, 255, 0.1);
    }
    
    • Has a border-radius, margin, color, cursor-pointer, font-size and padding.
    • Set Height and width for best aligning.
    • .operator {color: #6dee0a;} set this color for all operators

Step 3(JAVASCRIPT Code): Bring Calculator to life by implementing the calculator logic using JavaScript. Handle operations.

  1. 1. DOM Variables:
    • input: It will display answer of opertion.
    • buttons: It will handle all number and opraters.
    • string: In this variable add the number and operation the finally add last answer.
  2. CSS

    let input = document.getElementById("main-input")
    let buttons = document.querySelectorAll("button")
    let string = ""
    let arr = Array.from(buttons)
    arr.forEach((button) => {   
    button.addEventListener("click", (item) => {
    let clickedButton = item.target.innerHTML
    switch (clickedButton) {
    case "=":
    string = eval(string)
    input.value = string;
    string = ""
    break
                            
    case "AC":
    string = ""
    input.value = string
    break
                            
    case "DEL":
    string = string.slice(0, string.length - 1)
    input.value = string
    break
                            
    default:
    string += item.target.innerHTML
    input.value = string
    break
    }                        
    })
    })

Final Output: