Home  >  Article  >  Web Front-end  >  Build a Simple Calculator Website

Build a Simple Calculator Website

王林
王林Original
2024-08-23 14:33:27263browse

Build a Simple Calculator Website

Introduction

Hello, fellow developers! I'm thrilled to share my latest project: a Simple Calculator. This project is a perfect way to explore the basics of JavaScript, especially in handling mathematical operations, updating the DOM dynamically, and creating an interactive user interface. Whether you're new to web development or looking to sharpen your JavaScript skills, this Simple Calculator project is a great starting point.

Project Overview

The Simple Calculator is a web-based application that allows users to perform basic arithmetic operations like addition, subtraction, multiplication, and division. This project highlights how to create a responsive and user-friendly interface while efficiently handling user inputs to perform calculations.

Features

  • Intuitive Interface: Clean and straightforward design for easy interaction.
  • Basic Arithmetic Operations: Supports addition, subtraction, multiplication, and division.
  • Clear and Delete Functionality: Users can clear the input or delete the last entry with a single click.
  • Responsive Design: Ensures consistent usability across different devices and screen sizes.

Technologies Used

  • HTML: Structures the web page and input elements.
  • CSS: Styles the calculator interface, ensuring a clean and responsive design.
  • JavaScript: Manages the calculation logic and user interactions.

Project Structure

Here's a quick look at the project structure:

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

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

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

    cd Simple-Calculator
    
  3. Run the project:

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

Usage

  1. Open the website in a web browser.
  2. Enter numbers and perform arithmetic operations using the calculator buttons.
  3. Use the "AC" button to clear all input, or "DEL" button to delete the last entry.
  4. Click the "=" button to see the result of the calculation.

Code Explanation

HTML

The index.html file provides the basic structure of the Simple Calculator, including the display area and buttons for arithmetic operations. Here’s a snippet:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
  93f0f5c25f18dab9d176bd4f6de5d30e
    7c8d9f814bcad6a1d7abe4eda5f773e5
    26faf3d1af674280d03ba217d87e9421
    b2386ffb911b14667cb8f0f91ea547a7Simple Calculator6e916e0f7d1e588d4f442bf645aedb2f
    af75c476cdb7e6c074ca6da9b40841de
    90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0
  9c3bca370b5104690d9ef395f2c5f8d1
  6c04bd5ca3fcae76e30b72ad730ca86d
    924ff17625d603f964501dd897c96cc6
      4a249f0d628e2318394fd9b75b4636b1Simple Calculator473f0a7621bec819994bb5020d29372a
    16b28748ea4df4d9c2150843fecfba68
    4883ec0eb33c31828b7c767c806e14c7
      484fa028d8c6f85de53c7938dec280ce
        81304ad60882cdf6a4703237057e689b
        dc6dce4a544fdca2df29d5ac0ea9906b
          88c70033a81354858c2d5d5f8799f2d2AC65281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2DEL65281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2%65281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2/65281c5ac262bf6d81768915a4a77ac0
        16b28748ea4df4d9c2150843fecfba68
        dc6dce4a544fdca2df29d5ac0ea9906b
          7a766ea05486930017614b5a53f01815765281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815865281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815965281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2*65281c5ac262bf6d81768915a4a77ac0
        16b28748ea4df4d9c2150843fecfba68
        dc6dce4a544fdca2df29d5ac0ea9906b
          7a766ea05486930017614b5a53f01815465281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815565281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815665281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2-65281c5ac262bf6d81768915a4a77ac0
        16b28748ea4df4d9c2150843fecfba68
        dc6dce4a544fdca2df29d5ac0ea9906b
          7a766ea05486930017614b5a53f01815165281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815265281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815365281c5ac262bf6d81768915a4a77ac0
          88c70033a81354858c2d5d5f8799f2d2+65281c5ac262bf6d81768915a4a77ac0
        16b28748ea4df4d9c2150843fecfba68
        dc6dce4a544fdca2df29d5ac0ea9906b
          7a766ea05486930017614b5a53f018150065281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815065281c5ac262bf6d81768915a4a77ac0
          7a766ea05486930017614b5a53f01815.65281c5ac262bf6d81768915a4a77ac0
          7c8d25676e5efa114cb91329eb362165=65281c5ac262bf6d81768915a4a77ac0
        16b28748ea4df4d9c2150843fecfba68
      16b28748ea4df4d9c2150843fecfba68
    16b28748ea4df4d9c2150843fecfba68
    ffd6ba4147bda351239915f463e46e38
      e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
    16b28748ea4df4d9c2150843fecfba68
  36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

The styles.css file styles the Simple Calculator, providing a modern and user-friendly layout. Here are some key styles:

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap');

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

body {
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    background: linear-gradient(45deg, #0a0a0a, #3a4452);
}

.header {
    color: white;
    margin: 30px;
    text-align: center;
}

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

input {
    width: 320px;
    border: none;
    padding: 24px;
    margin: 10px;
    background: transparent;
    box-shadow: 0px 3px 15px rgba(84, 84, 84, 0.1);
    font-size: 40px;
    text-align: right;
    cursor: pointer;
    color: #ffffff;
}

input::placeholder {
    color: #ffffff;
}

button {
    border: none;
    width: 60px;
    height: 60px;
    margin: 10px;
    border-radius: 50%;
    background: transparent;
    color: #ffffff;
    font-size: 20px;
    box-shadow: -8px -8px 15px rgba(255, 255, 255, 0.1);
    cursor: pointer;
}

.equalBtn {
    background-color: #fb7c14;
}

.operator {
    color: #6dee0a;
}

.footer {
    color: white;
    margin: 50px;
    text-align: center;
}

JavaScript

The script.js file manages the logic for performing calculations and handling user interactions. Here’s a snippet:

let input = document.getElementById("inputBox");
let buttons = document.querySelectorAll("button");

let string = "";
let arr = Array.from(buttons);

arr.forEach((button) => {
  button.addEventListener("click", (e) => {
    if (e.target.innerHTML === "=") {
      string = eval(string);
      input.value = string;
    } else if (e.target.innerHTML === "AC") {
      string = "";
      input.value = string;
    } else if (e.target.innerHTML === "DEL") {
      string = string.substring(0, string.length - 1);
      input.value = string;
    } else {
      string += e.target.innerHTML;
      input.value = string;
    }
  });
});

Live Demo

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

Conclusion

Creating this Simple Calculator was a rewarding experience that deepened my understanding of JavaScript and how to build interactive web applications. I hope this project encourages you to experiment with your own JavaScript projects. Happy coding!

Credits

This project was developed as part of my journey to enhance my web development skills, focusing on JavaScript and user interaction.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Build a Simple 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