Hello, fellow developers! I'm thrilled to share my latest project: aSimple 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.
TheSimple Calculatoris 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.
Here's a quick look at the project structure:
Simple-Calculator/ ├── index.html ├── styles.css └── script.js
To get started with the project, follow these steps:
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Simple-Calculator.git
Open the project directory:
cd Simple-Calculator
Run the project:
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:
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; }
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; } }); });
You can check out the live demo of the Simple Calculator here.
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!
This project was developed as part of my journey to enhance my web development skills, focusing on JavaScript and user interaction.
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!