-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (73 loc) · 2.22 KB
/
script.js
File metadata and controls
88 lines (73 loc) · 2.22 KB
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
var button = document.getElementsByClassName("button");
var display = document.getElementById("display");
var op1=0;
var op2= null;
var operator = null;
var flag = false;
for(var i=0; i<button.length ; i++){
button[i].addEventListener('click',function(){
var value = this.getAttribute('data-value');
if(value== '+' || value == '*' || value == '-'){
flag=false;
operator= value;
op1= parseFloat(display.textContent);
display.innerText='';
}
else if(value == '.'){
var a=display.textContent;
if(flag==true){
alert("Invalid Input");
display.innerText= Error;
flag=false;
}
else{
a=a+".";
display.innerText=a;
flag=true;
}
}
else if(value == '/'){
flag=false;
operator= value;
op1= parseFloat(display.textContent);
display.innerText=' ';
}
else if(value == '+/-'){
flag=false;
var a=parseFloat(display.textContent);
a=a*(-1);
display.innerText=a;
}
else if(value == '%'){
flag=false;
var a=parseFloat(display.textContent);
if(a== null)
display.innerText = Error;
else{
a=a/100;
display.innerText = a;
}
}
else if(value == '='){
flag=false;
op2= parseFloat(display.textContent);
if(op1 == 0 && operator =='/' && op2== 0 ){
display.innerText= 'Error';
}
else if(operator =='/' && op2== 0){
display.innerText='Infinite';
}
else{
display.innerText='';
var ans = eval(op1+" "+ operator+" "+ op2);
display.innerText = ans;
}
}
else if(value == 'AC'){
display.innerText='';
}
else{
display.innerText += value;
}
});
}