Skip to content

Instantly share code, notes, and snippets.

@decagondev
Created January 10, 2026 02:56
Show Gist options
  • Select an option

  • Save decagondev/484807a6a3ff5afa9153ee276c552f33 to your computer and use it in GitHub Desktop.

Select an option

Save decagondev/484807a6a3ff5afa9153ee276c552f33 to your computer and use it in GitHub Desktop.
<!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;
max-width: 400px;
margin: 50px auto;
padding: 20px;
background-color: #f5f5f5;
}
.calculator {
background-color: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h2 {
margin-top: 0;
color: #333;
}
label {
display: block;
margin-top: 15px;
margin-bottom: 5px;
color: #555;
font-weight: bold;
}
select, input {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box;
}
button {
width: 100%;
padding: 12px;
margin-top: 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
font-weight: bold;
}
button:hover {
background-color: #45a049;
}
#result {
margin-top: 20px;
padding: 15px;
background-color: #e8f5e9;
border-radius: 4px;
font-size: 18px;
font-weight: bold;
color: #2e7d32;
text-align: center;
min-height: 20px;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Calculator</h2>
<label for="operation">Operation:</label>
<select id="operation">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="multiply">Multiply</option>
<option value="divide">Divide</option>
</select>
<label for="op1">Number 1:</label>
<input type="number" id="op1" step="any" placeholder="Enter first number">
<label for="op2">Number 2:</label>
<input type="number" id="op2" step="any" placeholder="Enter second number">
<button onclick="calculate()">Calculate</button>
<div id="result"></div>
</div>
<script>
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
return "Cannot divide by zero";
}
return a / b;
}
function calculate() {
const operation = document.getElementById('operation').value;
const op1 = parseFloat(document.getElementById('op1').value);
const op2 = parseFloat(document.getElementById('op2').value);
const resultDiv = document.getElementById('result');
if (isNaN(op1) || isNaN(op2)) {
resultDiv.textContent = "Please enter valid numbers";
return;
}
let result;
if (operation === 'add') {
result = add(op1, op2);
} else if (operation === 'subtract') {
result = subtract(op1, op2);
} else if (operation === 'multiply') {
result = multiply(op1, op2);
} else if (operation === 'divide') {
result = divide(op1, op2);
}
resultDiv.textContent = "Result: " + result;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment