gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Code a calculator in html
- User
Code a calculator in html
- Assistant
Code a calculator in html
Certainly! Here's a simple calculator built with HTML, CSS, and JavaScript. This calculator can perform basic arithmetic operations like addition, subtraction, multiplication, and division.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Simple Calculator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.calculator {
background-color: #222;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 15px #000;
width: 320px;
}
.display {
background-color: #444;
color: #fff;
font-size: 2em;
padding: 10px;
border-radius: 5px;
text-align: right;
margin-bottom: 15px;
min-height: 50px;
word-wrap: break-word;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
font-size: 1.2em;
padding: 15px;
border: none;
border-radius: 5px;
background-color: #666;
color: white;
cursor: pointer;
transition: background-color 0.2s ease;
}
button:hover {
background-color: #888;
}
.operator {
background-color: #f57c00;
}
.operator:hover {
background-color: #fb8c00;
}
.equals {
background-color: #388e3c;
grid-column: span 2;
}
.equals:hover {
background-color: #43a047;
}
.clear {
background-color: #d32f2f;
}
.clear:hover {
background-color: #e53935;
}
</style>
</head>
<body>
<div class="calculator">
<div id="display" class="display">0</div>
<div class="buttons">
<button class="clear" id="clear">C</button>
<button id="backspace">⌫</button>
<button class="operator" data-operator="/">÷</button>
<button class="operator" data-operator="*">×</button>
<button data-number="7">7</button>
<button data-number="8">8</button>
<button data-number="9">9</button>
<button class="operator" data-operator="-">−</button>
<button data-number="4">4</button>
<button data-number="5">5</button>
<button data-number="6">6</button>
<button class="operator" data-operator="+">+</button>
<button data-number="1">1</button>
<button data-number="2">2</button>
<button data-number="3">3</button>
<button class="equals" id="equals">=</button>
<button data-number="0" style="grid-column: span 2;">0</button>
<button data-number=".">.</button>
</div>
</div>
<script>
const display = document.getElementById('display');
const buttons = document.querySelectorAll('button');
let currentInput = '';
let lastInput = '';
function updateDisplay() {
display.textContent = currentInput || '0';
}
function isOperator(char) {
return ['+', '-', '*', '/'].includes(char);
}
buttons.forEach(button => {
button.addEventListener('click', () => {
const number = button.getAttribute('data-number');
const operator = button.getAttribute('data-operator');
const id = button.id;
if (number !== null) {
// Prevent multiple decimals in a number
if (number === '.') {
// If current input is empty or ends with operator, prepend 0
if (currentInput === '' || isOperator(currentInput.slice(-1))) {
currentInput += '0';
}
// Don't allow multiple decimals in the current number segment
const parts = currentInput.split(/[\+\-\*\/]/);
const lastPart = parts[parts.length - 1];
if (lastPart.includes('.')) {
return; // ignore additional decimal
}
}
currentInput += number;
updateDisplay();
} else if (operator !== null) {
// Prevent two operators in a row
if (currentInput === '') {
// If empty and user presses -, allow negative number
if (operator === '-') {
currentInput = '-';
updateDisplay();
}
return;
}
if (isOperator(currentInput.slice(-1))) {
// Replace last operator
currentInput = currentInput.slice(0, -1) + operator;
} else {
currentInput += operator;
}
updateDisplay();
} else if (id === 'clear') {
currentInput = '';
updateDisplay();
} else if (id === 'backspace') {
currentInput = currentInput.slice(0, -1);
updateDisplay();
} else if (id === 'equals') {
if (currentInput === '') return;
// Remove trailing operator if any
if (isOperator(currentInput.slice(-1))) {
currentInput = currentInput.slice(0, -1);
}
try {
// Use Function constructor instead of eval for safer evaluation
// (Note: still be cautious with user input in production)
const result = Function('"use strict";return (' + currentInput + ')')();
currentInput = String(result);
updateDisplay();
} catch (e) {
display.textContent = 'Error';
currentInput = '';
}
}
});
});
</script>
</body>
</html>
Function constructor for a bit more safety than eval.Open this file in a browser and you have a working calculator. Let me know if you'd like it extended with more features!