-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
284 lines (240 loc) · 11.7 KB
/
index.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<!-- Font Awesome -->
<link type="text/css" href="./vendor/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">
<!-- Pixel CSS -->
<link type="text/css" href="./minified_style.css" rel="stylesheet">
<style>
/* ac-clear and plus-minus */
.ac-clear, .plus-minus {
font: 1rem !important;
}
div >div.btn {
padding: 0.4rem !important;
}
/* media query for screens with width smaller than 505 */
@media screen and (max-width: 505px) {
div >div.btn {
padding: 0.4rem !important;
}
.plus-minus {
font-size: 0.8rem !important;
/* place text vertically in the middle of the button */
line-height: 1.6rem !important;
width: 100%;
height: 100%;
}
}
div.alert{
overflow-y: auto;
}
.result {
white-space: nowrap;
}
div.main{
/* vertically center */
display: flex;
justify-content: center;
align-items: center;
height: 95vh;
margin: 0;
}
</style>
</head>
<body>
<!-- calculator -->
<div class="row justify-content-center small main">
<div class="col-10 col-md-8 col-lg-5">
<div class="card bg-primary shadow-soft border-light rounded">
<!-- result screen -->
<div class="card-body">
<div class="row">
<div class="col-12 ">
<!-- shadow inside -->
<div class="alert alert-secondary shadow-inset " role="alert">
<!-- <input type="text" class="result form-control h1" id="result" disabled value = "0"> -->
<span class="result alert-inner--text h1" >0</span>
</div>
<!-- result screen for showing result -->
<!-- disabled input -->
<!-- <input type="text" class="result form-control" id="result" disabled value = "0"> -->
</div>
</div>
<!-- number pad -->
<div class="row p-3">
<div class="col-12">
<div class="row mt-1">
<div class="col-3 p-1">
<div type="button" class="ac-clear btn btn-primary btn-lg btn-block rounded-pill font-small">C
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="plus-minus btn btn-primary btn-lg btn-block rounded-pill">+/-</div>
</div>
<div class="col-3 p-1">
<div type="button" class="percent btn btn-primary btn-lg btn-block rounded-pill">%
</div>
</div>
<div class="col-3 p-1">
<div type="button"
class=" operator divide btn btn-primary btn-lg btn-block text-danger rounded-pill">/</div>
</div>
</div>
<div class="row mt-1">
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill ">7
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">8
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">9
</div>
</div>
<div class="col-3 p-1">
<!-- add multiply icon using font awesome -->
<div type="button"
class="operator multiply text-danger btn btn-primary btn-lg btn-block rounded-pill">x</div>
</div>
</div>
<div class="row mt-1">
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">4
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">5
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">6
</div>
</div>
<div class="col-3 p-1">
<div type="button"
class="operator minus btn btn-primary btn-lg text-danger btn-block rounded-pill">-</div>
</div>
</div>
<div class="row mt-1">
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">1
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">2
</div>
</div>
<div class="col-3 p-1">
<div type="button" class="number btn btn-primary btn-lg btn-block rounded-pill">3
</div>
</div>
<div class="col-3 p-1">
<div type="button"
class="operator plus btn btn-primary btn-lg btn-block text-danger rounded-pill">+</div>
</div>
</div>
<div class="row mt-1">
<div class="col-6 p-1">
<div type="button"
class="number zero btn btn-primary btn-lg btn-block rounded-pill">0</div>
</div>
<div class="col-3 p-1">
<div type="button" class="dot btn btn-primary btn-lg btn-block rounded-pill">.</div>
</div>
<div class="col-3 p-1">
<div type="button" class="equal btn btn-primary text-danger btn-lg btn-block rounded-pill">=
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- add script -->
<script>
const result = document.querySelector('.result');
// select all .number and add event
const numbers = document.querySelectorAll(".number");
numbers.forEach((number) => {
number.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
if (result.innerText == 0) {
result.innerText = event.target.innerText;
} else {
result.innerText += event.target.innerText;
}
});
});
const four_operators = ["+", "-", "*", "/"];
// select all .operator and add event
const operators = document.querySelectorAll(".operator");
operators.forEach((operator) => {
operator.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// if result contains operator, solve it
if (result.innerText.includes("+") || result.innerText.includes("-") || result.innerText.includes("*") || result.innerText.includes("/")) {
result.innerText = eval(result.innerText);
}
// if multiply, convert to *
if (event.target.innerText == "x") {
result.innerText += "*";
} else {
result.innerText += event.target.innerText;
}
});
});
// add event to dot
const dot = document.querySelector(".dot");
dot.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
result.innerText += event.target.innerText;
});
// ac clear
const ac = document.querySelector(".ac-clear");
ac.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
result.innerText = 0;
});
// plus minus
const plus_minus = document.querySelector(".plus-minus");
plus_minus.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
result.innerText = -result.innerText;
});
// percent
const percent = document.querySelector(".percent");
percent.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
result.innerText = eval(result.innerText) / 100;
});
// equal button
const equal = document.querySelector(".equal");
equal.addEventListener("click", (event) => {
// print the innertext of the button
console.log(event.target.innerText);
// append to screen
result.innerText = eval(result.innerText);
});
</script>
</body>
</html>