-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.html
202 lines (178 loc) · 4.82 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<html>
<head>
<style>
#calculator {
width: 300px;
padding: 8px;
border: 1px solid #bbb;
box-shadow: 1px 1px 5px 0px #00000080;
}
#display {
margin-bottom: 4px;
height: 40px;
border: 0;
background-color: #ddd;
text-align: right;
font-size: 2rem;
font-weight: bold;
padding: 8px;
font-family: 'Courier New', Courier, monospace;
}
.buttons {
padding: 0;
margin: 0;
width: 100%;
}
.buttons button {
width: 24%;
min-height: 50px;
padding: 8px;
margin: 0;
border: 0;
margin-bottom: 4px;
cursor: pointer;
opacity: 0.7;
font-weight: bold;
}
.buttons button:hover {
opacity: 1;
}
#btn-div, #btn-mult, #btn-sub, #btn-sum, #btn-equal {
background-color: #e9d994;
}
#btn-percent, #btn-ce, #btn-c {
background-color: #ccc;
}
</style>
</head>
<body>
<main>
<div id="calculator">
<div id="display"></div>
<div class="buttons">
<div>
<button id="btn-percent">%</button>
<button id="btn-ce">CE</button>
<button id="btn-c">C</button>
<button id="btn-div">/</button>
</div>
<div>
<button id="btn-7">7</button>
<button id="btn-8">8</button>
<button id="btn-9">9</button>
<button id="btn-mult">x</button>
</div>
<div>
<button id="btn-4">4</button>
<button id="btn-5">5</button>
<button id="btn-6">6</button>
<button id="btn-sub">-</button>
</div>
<div>
<button id="btn-1">1</button>
<button id="btn-2">2</button>
<button id="btn-3">3</button>
<button id="btn-sum">+</button>
</div>
<div>
<button id="">_</button>
<button id="btn-0">0</button>
<button id="btn-point">.</button>
<button id="btn-equal">=</button>
</div>
</div>
</div>
</main>
</body>
<script>
const display = document.getElementById('display');
const btn_percent = document.getElementById('btn-percent');
const btn_ce = document.getElementById('btn-ce');
const btn_c = document.getElementById('btn-c');
const btn_0 = document.getElementById('btn-0');
const btn_1 = document.getElementById('btn-1');
const btn_2 = document.getElementById('btn-2');
const btn_3 = document.getElementById('btn-3');
const btn_4 = document.getElementById('btn-4');
const btn_5 = document.getElementById('btn-5');
const btn_6 = document.getElementById('btn-6');
const btn_7 = document.getElementById('btn-7');
const btn_8 = document.getElementById('btn-8');
const btn_9 = document.getElementById('btn-9');
const btn_div = document.getElementById('btn-div');
const btn_mult = document.getElementById('btn-mult');
const btn_sub = document.getElementById('btn-sub');
const btn_sum = document.getElementById('btn-sum');
const btn_equal = document.getElementById('btn-equal');
const btn_point = document.getElementById('btn-point');
btn_percent.onclick = function() {
addToDisplay('%');
}
btn_ce.onclick = function() {
clearDisplay('ce');
}
btn_c.onclick = function() {
clearDisplay('c');
}
btn_0.onclick = function() {
addToDisplay('0');
}
btn_1.onclick = function() {
addToDisplay('1');
}
btn_2.onclick = function() {
addToDisplay('2');
}
btn_3.onclick = function() {
addToDisplay('3');
}
btn_4.onclick = function() {
addToDisplay('4');
}
btn_5.onclick = function() {
addToDisplay('5');
}
btn_6.onclick = function() {
addToDisplay('6');
}
btn_7.onclick = function() {
addToDisplay('7');
}
btn_8.onclick = function() {
addToDisplay('8');
}
btn_9.onclick = function() {
addToDisplay('9');
}
btn_1.onclick = function() {
addToDisplay('1');
}
btn_div.onclick = function() {
addToDisplay('/');
}
btn_mult.onclick = function() {
addToDisplay('*');
}
btn_sub.onclick = function() {
addToDisplay('-');
}
btn_sum.onclick = function() {
addToDisplay('+');
}
btn_equal.onclick = function() {
calculate();
}
btn_point.onclick = function() {
addToDisplay('.');
}
const addToDisplay = function(value) {
display.innerHTML += value;
}
const calculate = function() {
display.innerHTML = eval(display.innerHTML);
}
const clearDisplay = function(value) {
display.innerHTML = '';
}
</script>
</html>