Make me a calculator with HTML, CSS,JAVASCRIPT
- User
Make me a calculator with HTML, CSS,JAVASCRIPT
- Assistant
Make me a calculator with HTML, CSS,JAVASCRIPT
Here is a simple calculator implemented using HTML, CSS, and JavaScript. The calculator can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
<style>
#calculator {
width: 180px;
margin: 100px auto;
padding: 10px;
background-color: #f2f2f2;
border-radius: 5px;
box-shadow: 0 0 10px gray;
}
.display {
width: 100%;
padding: 10px;
margin-bottom: 10px;
background-color: #fff;
border: 1px solid #ccc;
text-align: right;
font-size: 18px;
}
.button {
width: 40px;
height: 40px;
margin: 5px;
font-size: 18px;
}
.button-row {
text-align: center;
}
</style>
</head>
<body>
<div id="calculator">
<input id="display" class="display" type="text" readonly>
<div class="button-row">
<button class="button" onclick="clearDisplay()">C</button>
<button class="button" onclick="updateDisplay('/')">/</button>
<button class="button" onclick="updateDisplay('*')">*</button>
<button class="button" onclick="updateDisplay('7')">7</button>
</div>
<div class="button-row">
<button class="button" onclick="updateDisplay('8')">8</button>
<button class="button" onclick="updateDisplay('9')">9</button>
<button class="button" onclick="updateDisplay('-')">-</button>
<button class="button" onclick="updateDisplay('4')">4</button>
</div>
<div class="button-row">
<button class="button" onclick="updateDisplay('5')">5</button>
<button class="button" onclick="updateDisplay('6')">6</button>
<button class="button" onclick="updateDisplay('+')">+</button>
<button class="button" onclick="updateDisplay('1')">1</button>
</div>
<div class="button-row">
<button class="button" onclick="updateDisplay('2')">2</button>
<button class="button" onclick="updateDisplay('3')">3</button>
<button class="button" onclick="calculateResult()">=</button>
<button class="button" onclick="updateDisplay('0')">0</button>
</div>
</div>
<script>
function updateDisplay(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculateResult() {
try {
const display = document.getElementById('display');
display.value = eval(display.value);
} catch (error) {
alert('Invalid Expression');
clearDisplay();
}
}
// Prevent the display from being edited directly
document.getElementById('display').addEventListener('keydown', function(event) {
event.preventDefault();
});
</script>
</body>
</html>
This code sets up a simple calculator layout and uses JavaScript to perform the calculations. The buttons use onclick event handlers to update the display or perform an action. The updateDisplay() function appends the button value to the display. The clearDisplay() function resets the display, and the calculateResult() function evaluates the expression and updates the display with the result.
It is important to note that using eval() to calculate the result can lead to security risks; it should not be used in production code or with user-provided input that has not been thoroughly validated or sanitized. For a more secure application, consider using a dedicated math library or writing a parser for mathematical expressions.
Not yet included in a published release.