Creating a comprehensive calculator with all the features of commonly used calculators is a complex task and typically involves significant JavaScript programming. I can provide a simplified HTML and JavaScript code for a basic calculator with addition, subtraction, multiplication, and division operations:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basic Calculator</title>
<style>
.calculator {
width: 200px;
margin: 0 auto;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
input[type="text"] {
width: 100%;
margin-bottom: 10px;
}
input[type="button"] {
width: 48px;
height: 48px;
margin: 3px;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<input type="button" value="1" onclick="appendToDisplay('1')">
<input type="button" value="2" onclick="appendToDisplay('2')">
<input type="button" value="3" onclick="appendToDisplay('3')">
<input type="button" value="+" onclick="appendToDisplay('+')">
<input type="button" value="4" onclick="appendToDisplay('4')">
<input type="button" value="5" onclick="appendToDisplay('5')">
<input type="button" value="6" onclick="appendToDisplay('6')">
<input type="button" value="-" onclick="appendToDisplay('-')">
<input type="button" value="7" onclick="appendToDisplay('7')">
<input type="button" value="8" onclick="appendToDisplay('8')">
<input type="button" value="9" onclick="appendToDisplay('9')">
<input type="button" value="*" onclick="appendToDisplay('*')">
<input type="button" value="C" onclick="clearDisplay()">
<input type="button" value="0" onclick="appendToDisplay('0')">
<input type="button" value="=" onclick="calculate()">
<input type="button" value="/" onclick="appendToDisplay('/')">
</div>
<script>
function appendToDisplay(value) {
document.getElementById('display').value += value;
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculate() {
try {
const result = eval(document.getElementById('display').value);
document.getElementById('display').value = result;
} catch (error) {
document.getElementById('display').value = 'Error';
}
}
</script>
</body>
</html>
```
This is a simple calculator that allows basic arithmetic calculations. It should give you a starting point to build upon if you need more advanced features or a full-featured calculator. Keep in mind that this code uses the `eval` function, which can be a security risk in real-world applications. For a secure and more complex calculator, additional coding is needed.


Comments
Post a Comment