I want html wyth calculator
- User
I want html wyth calculator
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I want html wyth calculator
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a simple HTML calculator with basic operations. It includes a display and buttons for digits, operators, decimal point, and clear function.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: #282c34;
font-family: Arial, sans-serif;
}
.calculator {
border-radius: 10px;
padding: 20px;
background: #1e1f22;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
width: 300px;
}
.display {
width: 100%;
margin-bottom: 15px;
padding: 15px;
text-align: right;
font-size: 24px;
background: #282c34;
color: white;
border: none;
border-radius: 5px;
outline: none;
height: 50px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.btn {
padding: 15px;
background: #4CAF50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 18px;
transition: background 0.3s ease;
}
.btn:hover {
background: #45a049;
}
.operator {
background: #ff9800;
}
.operator:hover {
background: #ff6f00;
}
.clear {
grid-column: span 2;
background: #f44336;
}
.clear:hover {
background: #d32f2f;
}
.equals {
grid-column: span 2;
}
.decimal {
background: #2196f3;
}
.decimal:hover {
background: #1976d2;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" class="display" id="display" readonly>
<div class="buttons">
<button class="btn clear" data-type="clear">C</button>
<button class="btn operator" data-type="operator" data-value="/">÷</button>
<button class="btn operator" data-type="operator" data-value="*">×</button>
<button class="btn" data-type="number" data-value="7">7</button>
<button class="btn" data-type="number" data-value="8">8</button>
<button class="btn" data-type="number" data-value="9">9</button>
<button class="btn operator" data-type="operator" data-value="-">-</button>
<button class="btn" data-type="number" data-value="4">4</button>
<button class="btn" data-type="number" data-value="5">5</button>
<button class="btn" data-type="number" data-value="6">6</button>
<button class="btn operator" data-type="operator" data-value="+">+</button>
<button class="btn" data-type="number" data-value="1">1</button>
<button class="btn" data-type="number" data-value="2">2</button>
<button class="btn" data-type="number" data-value="3">3</button>
<button class="btn equals" data-type="equals">=</button>
<button class="btn" data-type="number" data-value="0">0</button>
<button class="btn decimal" data-type="decimal" data-value=".">.</button>
</div>
</div>
<script>
const display = document.getElementById('display');
let currentOperand = '';
let previousOperand = '';
let operator = null;
document.querySelector('.buttons').addEventListener('click', function(event) {
const target = event.target;
if (!target.matches('button')) return;
const { type, value } = target.dataset;
if (type === 'number') {
handleNumber(value);
} else if (type === 'operator') {
handleOperator(value);
} else if (type === 'decimal') {
handleDecimal();
} else if (type === 'clear') {
clear();
} else if (type === 'equals') {
compute();
}
});
function handleNumber(num) {
if (operator && currentOperand === '') return;
currentOperand += num;
display.value = currentOperand;
}
function handleOperator(op) {
if (previousOperand === '' && currentOperand === '') return;
if (previousOperand !== '' && currentOperand === '') {
operator = op;
return;
}
if (operator !== null) compute();
previousOperand = currentOperand;
currentOperand = '';
operator = op;
}
function handleDecimal() {
if (currentOperand.includes('.')) return;
currentOperand += '.';
}
function compute() {
let computation;
const prev = parseFloat(previousOperand);
const current = parseFloat(currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (operator) {
case '+':
computation = prev + current;
break;
case '-':
computation = prev - current;
break;
case '*':
computation = prev * current;
break;
case '/':
computation = prev / current;
break;
default:
return;
}
currentOperand = computation.toString();
display.value = currentOperand;
previousOperand = '';
operator = null;
}
function clear() {
currentOperand = '';
previousOperand = '';
operator = null;
display.value = '0';
}
</script>
</body>
</html>
= to see the resultC to clear the display. for decimal numbersThis is a basic implementation that should work in modern browsers. Let me know if you want me to explain any part in more detail!