-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.html
321 lines (310 loc) · 10.4 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<!DOCTYPE html>
<html lang="en">
<head>
<title>Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale:1.0">
<style>
@font-face{
font-family:'dotty';
src: url("./font/dotty.ttf") format("truetype");
}
*{
margin:0;
padding:0;
box-sizing:border-box;
}
body{
min-height:100vh;
width:100vw;
background-color:rgb(250, 235, 215);
display:flex;
justify-content: center;
align-items:center;
}
#calcbody{
/* position: relative; */
width:40vh;
margin:auto;
padding: 0.2em;
border-radius: 1em;
gap: 0.1em;
/* grid-layout */
display:grid;
grid-template-columns: repeat(4,1fr);
grid-template-rows: 5vh 25vh repeat(5, 8vh);
grid-template-areas:"theme_sel theme_sel . ."
"dis dis dis dis"
"plus minus mul backSlash"
"k7 k8 k9 allClear"
"k4 k5 k6 ans"
"k1 k2 k3 ans"
"k0 point per devide";
}
#theme_sel{
background:none;
border:none;
color:rgb(255, 255, 255);
font-weight:900;
margin-left:2vh;
}
#theme_sel > option{
background:none;
}
#display{
grid-area: dis;
text-align:right;
font-family: "dotty";
/*flex setting*/
display:flex;
justify-content:flex-end;
align-items:flex-end;
}
#theme_sel{grid-area: theme_sel;}
#plus{ grid-area:plus;}
#minus{grid-area: minus;}
#mul{ grid-area: mul;}
#devide{ grid-area:devide;}
#per{ grid-area:per;}
#ans{grid-area:ans;}
#point{grid-area:point;}
#allClear{grid-area:allClear;}
#backSlash{grid-area:backSlash;}
#key1{ grid-area: k1;}
#key2{ grid-area: k2;}
#key3{ grid-area: k3;}
#key4{ grid-area: k4;}
#key5{ grid-area: k5;}
#key6{ grid-area: k6;}
#key7{ grid-area: k7;}
#key8{ grid-area: k8;}
#key9{ grid-area: k9;}
#key0{ grid-area: k0;}
.numKey{
/* flex-lalyout */
display: flex;
align-items: center;
justify-content: center;
}
/*theme setup start*/
.theme1 {
background-color:rgb(238, 130, 238);
border: .2em rgba(238, 130, 238, 0.6) solid;
}
.theme1 > .numKey{
color: rgb(255, 255, 255);
background-color: rgba(245, 71, 245, 0.8);
border: 2px rgb(246, 180, 246) solid;
border-radius: .5em;
font-size:larger;
font-weight: 900;
}
.theme1 > .numKey:hover{
color:aqua;
background-color:rgba(245, 71, 245, 1);
}
.theme1 > #display{
background-color:rgba(0, 255, 255, 0.736);
border-radius: 0.2em;
padding: .1em;
color:white;
font-size:15vh;
}
.theme2 {
background-color:#36c9ff5b;/*rgb(131, 150, 164); */
backdrop-filter: blur(20px);
border: 2px #fff solid;
box-shadow: 3px 3px 3px 3px #00001d4c;
}
.theme2 > .numKey{
background-color: rgba(54, 54, 54, 0.2);
width:100%;
border-radius:50%;
}
.theme2 > #display{
background-color:rgba(31, 42, 50, 0.3);
border-radius: 10px;
color:rgba(0, 0, 0, 0.945);
font-size:15vh;
}
.theme3{
background-color:rgb(168, 247, 168);
}
.theme3 > .numKey{
color:rgb(255, 255, 255);
border-radius: .5em;
border:none;
font-weight: 900;
background:none;
/*rgba(168, 247, 168, .2);*/
}
.theme3 > .numKey:hover{
background-color:rgba(121, 242, 121, 0.8);
}
.theme3 > #display{
font-size:5em;
}
/*theme setup end*/
</style>
</head>
<body>
<div class="theme2" id="calcbody">
<select id="theme_sel" name="themeSelector" size='1'>
<option value="1">Theme 1</option>
<option value="2">Theme 2</option>
<option value="3">Theme 3</option>
</select>
<div name="" id="display" ></div>
<button class="numKey" id="plus" value="+">+</button>
<button class="numKey" id="minus">-</button>
<button class="numKey" id="mul">x</button>
<button class="numKey" id="devide">/</button>
<button class="numKey" id="per">%</button>
<button class="numKey" id="ans">=</button>
<button class="numKey" id="point">.</button>
<button class="numKey" id="allClear">AC</button>
<button class="numKey" id="backSlash"><-</button>
<button class="numKey" id="key1">1</button>
<button class="numKey" id="key2">2</button>
<button class="numKey" id="key3">3</button>
<button class="numKey" id="key4">4</button>
<button class="numKey" id="key5">5</button>
<button class="numKey" id="key6">6</button>
<button class="numKey" id="key7">7</button>
<button class="numKey" id="key8">8</button>
<button class="numKey" id="key9">9</button>
<button class="numKey" id="key0">0</button>
</div>
</div>
<script>
let keyList=[];
for (let i=0; i<10; i++){
keyList.push(document.getElementById("key"+i));
}
let value=['',''];
let ope='';
let finalAns='';
let dis=document.getElementById("display");
function updateDisplay() {
if (typeof finalAns === 'number' && !isNaN(finalAns) && isFinite(finalAns)) {
dis.innerHTML = `${value[0]} ${ope} ${value[1]}<br>${finalAns}`;
} else {
dis.innerText = `${value[0]} ${ope} ${value[1]}`;
}
}
function isEmpty(){
return value[0] === '';
}
function resetValue(){
value[0]='';
value[1]='';
ope='';
finalAns='';
}
/* function for 0-9key*/
for (let i=0; i<10; i++){
keyList[i].addEventListener("click", ()=>{
if(ope ==='')value[0]+=i;
else value[1]+=i;
updateDisplay();
});
}
/*event for mathomatic operation*/
let plus = document.getElementById("plus");
let minus = document.getElementById("minus");
let mul = document.getElementById("mul");
let devide = document.getElementById("devide");
let per = document.getElementById("per");
let ans = document.getElementById("ans");
let point = document.getElementById("point");
let backSlash = document.getElementById("backSlash");
let allClear = document.getElementById("allClear");
plus.addEventListener("click", ()=>{
if (value[0]=='') value[0]='+';
else ope="+";
updateDisplay();
});
minus.addEventListener("click", ()=>{
if (value[0]=='') value[0]='-';
else if(ope!=='') value[1]='-';
else ope="-";
updateDisplay();
});
mul.addEventListener("click", ()=>{
if (!isEmpty()) ope="x";
updateDisplay();
});
devide.addEventListener("click", ()=>{
if (!isEmpty()) ope='/';
updateDisplay();
})
per.addEventListener("click", ()=> {
if (!isEmpty()) ope="%";
updateDisplay();
});
/*Operaction of mathamic */
ans.addEventListener("click", ()=>{
let num1=Number(value[0]);
let num2=Number(value[1]);
switch(ope){
case '+':
finalAns=num1 + num2;
break;
case '-':
finalAns = num1 - num2;
break;
case 'x':
finalAns= num1 * num2;
break;
case '/':
finalAns= num1 / num2;
break;
case '%':
finalAns= (num2 / num1)*100;
break;
default:
finalAns="Error";
}
updateDisplay();
});
backSlash.addEventListener("click", ()=>{
console.log("bc");
if (finalAns!=0) {
finalAns -= (finalAns%10);
finalAns /=10;
if (finalAns==0) finalAns='';
}else if(typeof finalAns==='number' && finalAns==0){ finalAns='';
}else if(value[1]!='') value[1] = value[1].slice(0,-1);
else if(ope!='') ope='';
else if(value[0]!='') value[0] =value[0].slice(0, -1);
updateDisplay();
});
point.addEventListener("click", ()=>{
if (value[0]=='')
value[0]='.';
else if((!value[0].includes('.')) && value[1]=='' ){
value[0]+='.';
}else if(!value[1].includes('.'))
value[1]+='.';
updateDisplay();
});
allClear.addEventListener("click", ()=>{
resetValue();
updateDisplay();
});
/*theme setting*/
let calcBody= document.getElementById("calcbody");
document.getElementById("theme_sel").addEventListener("change", ()=>{
let thvalue=event.target.value;
console.log('before'+calcBody.classList);
calcBody.classList.forEach(clc=>{
if(clc.startsWith('theme')){
calcBody.classList.remove(clc);
}
});
calcBody.classList.add('theme'+thvalue);
console.log(event.target.value);
console.log('after'+calcBody.classList[0]);
});
</script>
</body>
</html>