forked from quandz24-ui/OriginOS_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.js
More file actions
60 lines (51 loc) · 1.47 KB
/
calc.js
File metadata and controls
60 lines (51 loc) · 1.47 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
let display_calc = document.getElementById("display_calc");
function append_calc(value) {
const current = display_calc.innerText;
if (current === "Error") return;
else if (current === "Infinity") return;
else if (current === "NaN") return;
if (value === "+/-") {
if (current.startsWith("-")) {
display_calc.innerText = current.slice(1);
} else if (current !== "0") {
display_calc.innerText = "-" + current;
}
} else if (value === ".") {
if (current === "0") {
display_calc.innerText = "0.";
} else {
display_calc.innerText += ".";
}
} else {
if (current === "0") {
display_calc.innerText = value;
} else {
display_calc.innerText += value;
}
}
}
function clearDisplay_calc() {
display_calc.innerText = "0";
}
function backspace_calc() {
if (display_calc.innerText === "Error") return;
let text = display_calc.innerText;
if (text.length > 1) {
display_calc.innerText = text.slice(0, -1);
} else {
display_calc.innerText = "0";
}
}
function calculate_calc() {
if (display_calc.innerText === "Error") return;
else if (display_calc.innerText === "Infinity") return;
else if (display_calc.innerText === "NaN") return;
try {
const expression = display_calc.innerText
.replace(/:/g, "/")
.replace(/×/g, "*");
display_calc.innerText = eval(expression);
} catch {
display_calc.innerText = "Error";
}
}