-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic calculator.html
65 lines (60 loc) · 2.36 KB
/
basic 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
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<style>
.calculator {
width: 200px;
padding: 10px;
background-color: #f1f1f1;
border-radius: 5px;
}
.calculator input[type="text"] {
width: 100%;
margin-bottom: 10px;
padding: 5px;
}
.calculator input[type="button"] {
width: 48%;
padding: 5px;
}
.calculator input[type="button"]:nth-child(4),
.calculator input[type="button"]:nth-child(8),
.calculator input[type="button"]:nth-child(12),
.calculator input[type="button"]:nth-child(16) {
margin-right: 0;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<input type="button" value="7" onclick="appendToDisplay('7')">
<input type="button" value="8" onclick="appendToDisplay('8')">
<input type="button" value="9" onclick="appendToDisplay('9')">
<input type="button" value="/" onclick="appendToDisplay('/')">
<input type="button" value="4" onclick="appendToDisplay('4')">
<input type="button" value="5" onclick="appendToDisplay('5')">
<input type="button" value="6" onclick="appendToDisplay('6')">
<input type="button" value="*" onclick="appendToDisplay('*')">
<input type="button" value="1" onclick="appendToDisplay('1')">
<input type="button" value="2" onclick="appendToDisplay('2')">
<input type="button" value="3" onclick="appendToDisplay('3')">
<input type="button" value="-" onclick="appendToDisplay('-')">
<input type="button" value="0" onclick="appendToDisplay('0')">
<input type="button" value="." onclick="appendToDisplay('.')">
<input type="button" value="=" onclick="calculate()">
<input type="button" value="+" onclick="appendToDisplay('+')">
</div>
<script>
function appendToDisplay(value) {
document.getElementById('display').value += value;
}
function calculate() {
var display = document.getElementById('display').value;
var result = eval(display);
document.getElementById('display').value = result;
}
</script>
</body>
</html>