-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitchcalc.html
51 lines (49 loc) · 1.64 KB
/
switchcalc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Switch Calculator</title>
</head>
<body>
<input type="number" id="n1" name="n1">
<select id="operand" name="operand">
<option disabled selected value>--select an option--</option>
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" id="n2" name="n2">
<p id="output"></p>
<button onclick="run()">Calculate</button>
<script>
function run() {
var num1 = Number(document.getElementById("n1").value);
var num2 = Number(document.getElementById("n2").value);
var op = document.getElementById("operand").value;
var result = 0;
switch (op) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
default:
document.getElementById("output").innerText =
"Invalid option selected!";
return; // Immediately exit the function
}
document.getElementById("output").innerText =
num1 + " " + op + " " + num2 + " = " + result;
}
</script>
</body>
</html>