Calculator

HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Calculator</title> <link rel="stylesheet" href="styles.css"> </head> <body> <div class="calculator"> <div class="display" id="display">0</div> <div class="buttons"> <button class="btn" id="clear">AC</button> <button class="btn operator" id="modulo">%</button> <button class="btn operator" id="square">x²</button> <button class="btn operator" id="divide">/</button> <button class="btn" id="seven">7</button> <button class="btn" id="eight">8</button> <button class="btn" id="nine">9</button> <button class="btn operator" id="multiply">*</button> <button class="btn" id="four">4</button> <button class="btn" id="five">5</button> <button class="btn" id="six">6</button> <button class="btn operator" id="subtract">-</button> <button class="btn" id="one">1</button> <button class="btn" id="two">2</button> <button class="btn" id="three">3</button> <button class="btn operator" id="add">+</button> <button class="btn" id="zero">0</button> <button class="btn" id="decimal">.</button> <button class="btn" id="equals">=</button> </div> </div> <script src="script.js"></script> </body> </html>
CSS
body { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f0f0f0; font-family: Arial, sans-serif; } .calculator { width: 300px; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); } .display { width: 100%; height: 60px; background-color: #333; color: white; text-align: right; font-size: 2em; line-height: 60px; padding: 0 10px; border-radius: 5px; margin-bottom: 20px; overflow: hidden; } .buttons { display: grid; grid-template-columns: repeat(4, 1fr); grid-gap: 10px; } .btn { width: 100%; height: 60px; background-color: #f9f9f9; border: none; font-size: 1.5em; border-radius: 5px; cursor: pointer; transition: background-color 0.3s; } .btn:hover { background-color: #e0e0e0; } .operator { background-color: #ff9f43; color: white; } .operator:hover { background-color: #ff7f00; }
JAVASCRIPT
const display = document.getElementById('display'); let currentInput = ''; let previousInput = ''; let operator = ''; function updateDisplay(value) { display.innerText = value; } function clearDisplay() { currentInput = ''; previousInput = ''; operator = ''; updateDisplay('0'); } function calculate() { let result = 0; const num1 = parseFloat(previousInput); const num2 = parseFloat(currentInput); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; case '%': result = num1 % num2; break; case 'x²': result = num1 ** 2; break; default: return; } currentInput = result.toString(); updateDisplay(currentInput); previousInput = ''; operator = ''; } document.querySelectorAll('.btn').forEach(button => { button.addEventListener('click', () => { const value = button.innerText; if (!isNaN(value) || value === '.') { currentInput += value; updateDisplay(currentInput); } else if (value === 'AC') { clearDisplay(); } else if (value === '=') { if (previousInput !== '' && operator !== '') { calculate(); } } else if (value === 'x²') { operator = value; previousInput = currentInput; currentInput = ''; calculate(); } else { if (previousInput === '') { previousInput = currentInput; } else if (currentInput !== '') { calculate(); } operator = value; currentInput = ''; } }); });
Expand for more options Login