-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (54 loc) · 1.38 KB
/
script.js
File metadata and controls
57 lines (54 loc) · 1.38 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
let str = "";
let count_open = 0;
let count_close = 0;
buttonClicked = (id) => {
let a = document.getElementById(id);
if (a.innerHTML == "x") {
str = str + "*";
} else if (a.innerHTML == "÷") {
str = str + "/";
} else if (a.innerHTML == "AC") {
str = "";
} else if (a.innerHTML == "CE") {
str = str.slice(0, str.length - 1);
} else if (a.innerHTML == "=") {
let iterate = str.matchAll(/\(/g);
let arr = Array.from(iterate);
count_open = arr.length;
iterate = str.matchAll(/\)/g);
arr = Array.from(iterate);
count_close = arr.length;
while (count_close < count_open) {
str += ")";
count_close++;
}
try {
str = eval(str);
document.getElementById("AC").innerHTML = "AC";
} catch (error) {
document.getElementById("display").innerHTML = "Error";
document.getElementById("AC").innerHTML = "AC";
str = "";
return;
}
} else if (a.innerHTML == "(") {
if (str.length == 0) {
str = str + "(";
} else {
str = str + "*(";
}
} else if (a.innerHTML == ")") {
str = str + ")";
} else {
str = str + a.innerHTML;
}
if (a.innerHTML != "=") {
if (str.length == 0) {
document.getElementById("AC").innerHTML = "AC";
} else {
document.getElementById("AC").innerHTML = "CE";
}
}
let b = document.getElementById("display");
b.innerHTML = str;
};