gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
41+14
- User
41+14
- Assistant
41 + 14 equals 55.
- User
41+14
- Assistant
41 + 14 equals 55.
- User
14+14
- Assistant
14 + 14 equals 28.
- User
write code for calculator in html
- Assistant
41+14
41 + 14 equals 55.
41+14
41 + 14 equals 55.
14+14
14 + 14 equals 28.
write code for calculator in html
Sure! Below is a simple HTML and JavaScript calculator. You can copy and paste this code into an HTML file and open it in a web browser to see the calculator in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}
#calculator {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
input[type="text"] {
width: 100%;
height: 40px;
font-size: 24px;
text-align: right;
margin-bottom: 10px;
}
button {
width: 23%;
height: 40px;
font-size: 20px;
margin: 1%;
cursor: pointer;
}
</style>
</head>
<body>
<div id="calculator">
<input type="text" id="result" disabled>
<div>
<button onclick="clearResult()">C</button>
<button onclick="appendToResult('7')">7</button>
<button onclick="appendToResult('8')">8</button>
<button onclick="appendToResult('9')">9</button>
<button onclick="appendToResult('/')">/</button>
</div>
<div>
<button onclick="appendToResult('4')">4</button>
<button onclick="appendToResult('5')">5</button>
<button onclick="appendToResult('6')">6</button>
<button onclick="appendToResult('*')">*</button>
</div>
<div>
<button onclick="appendToResult('1')">1</button>
<button onclick="appendToResult('2')">2</button>
<button onclick="appendToResult('3')">3</button>
<button onclick="appendToResult('-')">-</button>
</div>
<div>
<button onclick="appendToResult('0')">0</button>
<button onclick="calculateResult()">=</button>
<button onclick="appendToResult('+')">+</button>
</div>
</div>
<script>
function appendToResult(value) {
document.getElementById('result').value += value;
}
function clearResult() {
document.getElementById('result').value = '';
}
function calculateResult() {
try {
const result = eval(document.getElementById('result').value);
document.getElementById('result').value = result;
} catch (error) {
document.getElementById('result').value = 'Error';
}
}
</script>
</body>
</html>
appendToResult function adds the clicked button's value to the input field.clearResult function clears the input field.calculateResult function evaluates the expression in the input field using eval() and displays the result.Note: Always be cautious when using eval() as it can execute arbitrary code. In this simple example, it works well for basic arithmetic.
Not yet included in a published release.