-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_program_01.html
More file actions
73 lines (70 loc) · 1.58 KB
/
js_program_01.html
File metadata and controls
73 lines (70 loc) · 1.58 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Functional Calculator</title>
<style>
body {
font-family: Arial,sans-serif;
text-align: center;
margin-top: 50px;
}
input,select, button {
padding: 10px;
margin: 10px;
font-size: 16px;
}
#result {
margin-top: 20px;
font-size: 20px;
font-weight: bold;
color: darkblue;
}
</style>
</head>
<body>
<h2>Functional Calculator</h2>
<input type="number" id="num1" placeholder="Enter first number">
<input type="number" id="num2" placeholder="Enter second number"><br>
<select id="operation">
<option value="add">Add (+)</option>
<option value="subtract">Subtract (-)</option>
<option value="multiply">Multiply (×)</option>
<option value="divide">Divide (÷)</option>
</select>
<button onclick="calculate()">Calculate</button>
<div id="result">Result: </div>
<script>
function calculate() {
const num1 = parseFloat(document.getElementById("num1").value);
const num2 = parseFloat(document.getElementById("num2").value);
const operation = document.getElementById("operation").value;
let result;
if (isNaN(num1) || isNaN(num2)) {
result = "Please enter valid numbers!";
} else {
switch (operation) {
case "add":
result = num1 + num2;
break;
case "subtract":
result = num1 - num2;
break;
case "multiply":
result = num1 * num2;
break;
case "divide":
if (num2 === 0) {
result = "Cannot divide by zero!";
} else {
result = num1 / num2;
}
break;
default:
result = "Invalid operation!";
}
}
document.getElementById("result").innerText = "Result: " + result;
}
</script>
</body>
</html>