Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (94 loc) · 3.65 KB
/
Copy pathapp.js
File metadata and controls
113 lines (94 loc) · 3.65 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
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
var sound = document.getElementById("sound")
function playSound() {
var audio = new Audio('./sound/pop2.mp3');
audio.play();
}
sound.addEventListener("click",playSound);
var buttons = document.getElementsByClassName("button");
var display = document.getElementById('display');
var operand1 = 0;
var operand2 = null;
var operator = null;
for(var i=0;i<buttons.length;i++) {
buttons[i].addEventListener('click',function() {
var value = this.getAttribute('data-value');
if(display.innerText=="inValid"||display.innerText=="Infinity") display.innerText= value;
else if(operand2==null&&operator!=null&&(value=='+'||value=='-'||value=='*'||value=='/'||value=='%')) {
display.innerText = display.innerText.substring(0,display.innerText.length-1) + value;
}
else display.innerText+=value;
if(value=='ac') {
display.innerText = '';
operand1=0;
operand2=null;
operator=null;
}
else if(value=="sign") {
operand1=parseFloat(display.innerText);
if(Number.isNaN(operand1)==true) {
operand1=0;
}
display.innerText = -operand1;
}
else if(value=='+'||value=='-'||value=='*'||value=='/'||value=='%') {
if(operand2!=null&&operator!=null) {
operand2 = parseFloat(operand2);
var string;
if(operator=='+') string= "operand1 + operand2";
else if(operator=='-') string= "operand1 - operand2";
else if(operator=='*') string= "operand1 * operand2";
else if(operator=='/') string= "operand1 / operand2";
else if(operator=='%') string= "operand1 % operand2";
var res = eval(string);
if(Number.isNaN(res)==true) {
display.innerText="inValid";
}
else display.innerText=res+value;
operand1 = parseFloat(res);
operand2=null;
operator=value;
}
else {
operand1 = parseFloat(display.innerText);
if(Number.isNaN(operand1)==true) {
operand1=0;
}
operator = value;
}
// display.innerText += value;
}
else if(value=='='){
if(operator!=null&&operand2!=null) {
// display.innerText=operand2;
operand2 = parseFloat(operand2);
var string;
if(operator=='+') string= "operand1 + operand2";
else if(operator=='-') string= "operand1 - operand2";
else if(operator=='*') string= "operand1 * operand2";
else if(operator=='/') string= "operand1 / operand2";
else if(operator=='%') string= "operand1 % operand2";
var res = eval(string);
if(Number.isNaN(res)==true) {
display.innerText="inValid";
}
else display.innerText=res;
operand1 = parseFloat(res);
operand2=null;
operator=null;
}
else {
display.innerText="inValid";
operand1=0;
operator=null;
}
}
else if(operator!=null) {
if(operand2==null) {
operand2=value;
}
else {
operand2 += value;
}
}
})
}