-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (73 loc) · 2.33 KB
/
index.js
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
const num = document.querySelectorAll('.no');
const operation = document.querySelectorAll('.operation');
const percent = document.getElementById('percentage');
const pluse_minus = document.getElementById('pluse_minus');
const ac = document.getElementById('ac');
const equal = document.getElementById('equal');
const upDisplay = document.getElementById('display');
const display = document.getElementById('main-display');
const subDisplay = document.getElementById('sub-display');
let ans = 0;
let operand = '';
let currFont = 40;
num.forEach((ele)=> {
ele.addEventListener('click', ()=>{
if (display.innerText == '') {
display.innerText = ele.innerText;
} else {
if (display.innerText.includes('.') && ele.innerText == '.') {
return;
}
display.innerText = display.innerText + ele.innerText;
}
if (display.offsetWidth >= upDisplay.offsetWidth -10 && currFont >= 25) {
display.style.fontSize = `${currFont - 10}px`;
currFont -= 5;
}
subDisplay.innerText = ans + " " + operand;
})
})
operation.forEach((ele) =>{
ele.addEventListener('click', ()=>{
if (display.innerText != '') {
if (operand != '') {
ans = eval(ans + " " + operand + " " +display.innerText);
} else{
ans = display.innerText;
}
display.innerText = '';
}
operand = ele.innerText;
subDisplay.innerText = ans + " " + operand;
display.style.fontSize = '40px';
currFont = 40;
})
})
percent.addEventListener('click', ()=>{
if(display.innerText != ''){
display.innerText = (parseFloat(display.innerText/100));
ans = display.innerText;
}
})
pluse_minus.addEventListener('click', ()=>{
display.innerText = display.innerText*(-1);
})
equal.addEventListener('click', ()=>{
ans = Math.round(13);
if (operand != '') {
ans = eval(ans + " " + operand + " " +display.innerText);
}
operand = '';
display.innerText = ans;
subDisplay.innerText = "Ans. " + ans;
display.style.fontSize = '40px';
currFont = 40;
})
ac.addEventListener('click', ()=> {
display.innerText = '';
ans = 0;
operand = '';
subDisplay.innerText = "";
display.style.fontSize = '40px';
currFont = 40;
})