forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.html
81 lines (75 loc) · 2.56 KB
/
calculator.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
<style>
.calculator {
width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f7f7f7;
}
input[type="text"] {
width: 100%;
margin-bottom: 10px;
padding: 10px;
}
input[type="button"] {
width: 48px;
height: 48px;
font-size: 18px;
margin: 2px;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" readonly />
<input type="button" value="7" onclick="addToDisplay('7')" />
<input type="button" value="8" onclick="addToDisplay('8')" />
<input type="button" value="9" onclick="addToDisplay('9')" />
<input type="button" value="/" onclick="addToDisplay('/')" />
<br />
<input type="button" value="4" onclick="addToDisplay('4')" />
<input type="button" value="5" onclick="addToDisplay('5')" />
<input type="button" value="6" onclick="addToDisplay('6')" />
<input type="button" value="-" onclick="addToDisplay('-')" />
<br />
<input type="button" value="1" onclick="addToDisplay('1')" />
<input type="button" value="2" onclick="addToDisplay('2')" />
<input type="button" value="3" onclick="addToDisplay('3')" />
<input type="button" value="+" onclick="addToDisplay('+')" />
<br />
<input type="button" value="C" onclick="clearDisplay()" />
<input type="button" value="CE" onclick="clearDisplay1()" />
<input type="button" value="0" onclick="addToDisplay('0')" />
<input type="button" value="." onclick="addToDisplay('.')" />
<input type="button" value="=" onclick="calculate()" />
</div>
<script>
function addToDisplay(value) {
document.getElementById("display").value += value;
}
function clearDisplay() {
document.getElementById("display").value = "";
}
function calculate() {
try {
var result = eval(document.getElementById("display").value);
document.getElementById("display").value = result;
} catch (error) {
document.getElementById("display").value = "Error";
}
}
function clearDisplay1() {
var display = document.getElementById("display");
var currentValue = display.value;
if (currentValue.length > 0) {
display.value = currentValue.slice(0, -1); // Remove the last character
}
}
</script>
</body>
</html>