-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (37 loc) · 986 Bytes
/
Copy pathscript.js
File metadata and controls
42 lines (37 loc) · 986 Bytes
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
const disp = document.getElementById("disp");
let cur = "", prev = "", op = "";
document.querySelectorAll("[data-num]").forEach(btn => {
btn.onclick = () => {
cur += btn.dataset.num;
disp.textContent = cur;
};
});
document.querySelectorAll("[data-op]").forEach(btn => {
btn.onclick = () => {
if (!cur) return;
prev = cur;
op = btn.dataset.op;
cur = "";
};
});
document.querySelector("[data-act='eq']").onclick = () => {
if (!cur || !prev) return;
const a = +prev, b = +cur;
let res = 0;
if (op === "+") res = a + b;
if (op === "-") res = a - b;
if (op === "*") res = a * b;
if (op === "/") res = b === 0 ? "Err" : a / b;
disp.textContent = res;
cur = res.toString();
prev = "";
op = "";
};
document.querySelector("[data-act='clr']").onclick = () => {
cur = prev = op = "";
disp.textContent = "0";
};
document.querySelector("[data-act='del']").onclick = () => {
cur = cur.slice(0, -1);
disp.textContent = cur || "0";
};