-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
72 lines (66 loc) · 2.33 KB
/
script.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
topDisplay=document.querySelector('#top')
bottomDisplay=document.querySelector('#bottom')
function operate(operator,num1,num2){
if(operator=='+')
return num1+num2;
else if(operator=='*')
return num1*num2;
else if(operator=='-')
return num1-num2;
else if(num2!=0) return num1/num2;
}
buttons=document.querySelectorAll('button')
for(let i=0;i<buttons.length;i++){
buttons[i].addEventListener('click',function(){
let text=buttons[i].textContent;
if(buttons[i].id=='clear'){
topDisplay.textContent='';
bottomDisplay.textContent='';
}
else if(buttons[i].id=='delete'){
if((bottomDisplay.textContent).length!=0){
let y=bottomDisplay.textContent;
let l=y.length;
let z=y.substr(0,(l-1));
bottomDisplay.textContent=z;
}
else{
bottomDisplay.textContent=topDisplay.textContent;
topDisplay.textContent='';
let y=bottomDisplay.textContent;
let l=y.length;
let z=y.substr(0,(l-1));
bottomDisplay.textContent=z;
}
}
else if(buttons[i].className=='num'){
if(buttons[i].textContent=='.'){
let str=bottomDisplay.textContent;
let pos=str.search(/\./);
if(pos!=-1) bottomDisplay.textContent=str;
else bottomDisplay.textContent+=buttons[i].textContent;
}
else bottomDisplay.textContent+=buttons[i].textContent
}
else if(buttons[i].className=='operator'){
topDisplay.textContent=bottomDisplay.textContent+buttons[i].textContent;
bottomDisplay.textContent='';
}
else if(buttons[i].className=='solve'){
let x=Number(bottomDisplay.textContent);
let y=topDisplay.textContent
let len=y.length;
let z=Number(y.substr(0,len-1));
let op=y.substr(len-1,len);
if(op=='/'&&x==0){
topDisplay.textContent='SmartAss';
bottomDisplay.textContent='';
}
else{
let ans=operate(op,z,x);
topDisplay.textContent='';
bottomDisplay.textContent=ans;
}
}
})
}