-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
119 lines (107 loc) · 2.96 KB
/
app.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
class Calculator {
constructor(calcDisplay) {
this.display = calcDisplay;
this.clear();
}
clear() {
this.prevOperand = "";
this.currentOperand = "";
this.operation = undefined;
}
appendNumber(number) {
if (number === "." && this.currentOperand.includes(".")) return;
this.currentOperand += number;
}
chooseOperation(operation) {
if (this.currentOperand === "") return;
if (this.prevOperand !== "") {
this.compute();
}
this.operation = operation;
this.prevOperand = this.currentOperand;
this.currentOperand = "";
if (this.operation === "Sin" || this.operation === "Cos") this.compute();
}
compute() {
let computation;
const prev = parseFloat(this.prevOperand);
const current = parseFloat(this.currentOperand);
if (this.operation === "Cos") {
computation = Math.cos(prev);
this.currentOperand = computation;
this.operation = undefined;
this.prevOperand = "";
return;
}
if (this.operation === "Sin") {
computation = Math.sin(prev);
this.currentOperand = computation;
this.operation = undefined;
this.prevOperand = "";
return;
}
if (isNaN(prev) || isNaN(current)) return;
switch (this.operation) {
case "+":
computation = prev + current;
break;
case "-":
computation = prev - current;
break;
case "*":
computation = prev * current;
break;
case "/":
computation = prev + current;
break;
case "Sin":
computation = Math.sin(current);
break;
case "Cos":
computation = Math.cos(current);
break;
default:
break;
}
this.currentOperand = computation;
this.operation = undefined;
this.prevOperand = "";
}
updateDisplay() {
if (this.operation) {
this.display.innerText =
this.prevOperand.toString() +
this.operation +
this.currentOperand.toString();
} else {
this.display.innerText =
this.prevOperand.toString() + this.currentOperand.toString();
}
}
}
const numberButton = document.querySelectorAll("[data-number]");
const opButton = document.querySelectorAll("[data-operation]");
const equalButton = document.querySelector("[data-equals]");
const deleteButton = document.querySelector("[data-delete]");
const display = document.querySelector("[data-display]");
const calculator = new Calculator(display);
numberButton.forEach((button) => {
button.addEventListener("click", () => {
calculator.appendNumber(button.innerText);
calculator.updateDisplay();
});
});
opButton.forEach((op) => {
op.addEventListener("click", () => {
calculator.chooseOperation(op.innerText);
calculator.updateDisplay();
});
});
equalButton.addEventListener("click", () => {
calculator.compute();
calculator.updateDisplay();
});
deleteButton.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
});