-
Notifications
You must be signed in to change notification settings - Fork 0
/
cal.js
123 lines (117 loc) · 3.35 KB
/
cal.js
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
114
115
116
117
118
119
120
121
122
123
class cal {
constructor(pre, cur) {
this.preT = pre;
this.curT = cur;
this.clear();
}
clear() {
this.curop = "";
this.preop = "";
this.operation = undefined;
}
del() {
this.curop = this.curop.toString().slice(0, -1);
}
append(number) {
if (number == "." && this.curop.includes(".")) {
return;
}
//this.curop=number;
this.curop = this.curop.toString() + number.toString();
}
operator(operation) {
if (this.curop == "") {
return;
}
if (this.preop != "") {
this.compute();
}
this.operation = operation;
this.preop = this.curop;
this.curop = "";
}
compute() {
let computate;
const pre = parseFloat(this.preop);
const cur = parseFloat(this.curop);
if (isNaN(pre) || isNaN(cur)) {
return;
}
switch (this.operation) {
case "+":
computate = pre + cur;
break;
case "-":
computate = per - cur;
break;
case "*":
computate = pre * cur;
break;
case "÷":
computate = pre / cur;
break;
default:
return;
}
this.curop = computate;
this.operation = undefined;
this.preop = "";
}
display_num(number) {
const strnum = number.toString();
const intnum = parseFloat(strnum.split(".")[0]);
const decnum = strnum.split(".")[1];
const num = parseFloat(number);
let intdisplay;
if (isNaN(intnum)) {
intdisplay = "";
} else {
intdisplay = intnum.toLocaleString("en",{ maximumFractionDigits:1});
}
if (decnum != null) {
return `${intdisplay}.${decnum}`;
} else {
return (intdisplay);
}
}
display() {
this.curT.innerText = this.display_num(this.curop);
if (this.operation != null) {
this.preT.innerText = `${this.display_num(this.preop)}${this.operation}`;
} else {
this.preT.innerText = "";
}
}
}
const num = document.querySelectorAll("[data-number]");
const op = document.querySelectorAll("[data-operation]");
const c = document.querySelector("[data-clear]");
const eq = document.querySelector("[data-equal-operator]");
const pre = document.querySelector("[data-pre-operand]");
const cur = document.querySelector("[data-cur-operand]");
const del = document.querySelector("[data-delete]");
const calculator = new cal(pre, cur);
num.forEach((button) => {
button.addEventListener("click", () => {
calculator.append(button.innerText);
calculator.display();
});
});
op.forEach((button) => {
button.addEventListener("click", () => {
calculator.operator(button.innerText);
calculator.display();
});
});
eq.addEventListener("click", (button) => {
calculator.compute();
calculator.display();
});
c.addEventListener("click", (button) => {
calculator.clear();
calculator.display();
});
del.addEventListener("click", (button) => {
calculator.del();
calculator.display();
});