-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (106 loc) · 3.22 KB
/
script.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
class Calculator {
constructor(smallDisplayTextElement, bigDisplayTextElement) {
this.smallDisplayTextElement = smallDisplayTextElement;
this.bigDisplayTextElement = bigDisplayTextElement;
this.clear();
}
clear() {
this.smalldisplay = "";
this.bigdisplay = "";
this.operation = undefined;
}
deleteNumbers() {
this.bigdisplay = this.bigdisplay.toString().slice(0, -1);
}
appendNumber(number) {
if (number === "." && this.bigdisplay.includes(".")) return;
this.bigdisplay = this.bigdisplay.toString() + number.toString();
}
chooseOperation(operation) {
if (this.bigdisplay === "") return;
if (this.smalldisplay !== "") {
this.compute();
}
this.operation = operation;
this.smalldisplay = this.bigdisplay;
this.bigdisplay = "";
}
compute() {
let computation;
const prev = parseFloat(this.smalldisplay);
const current = parseFloat(this.bigdisplay);
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;
default:
return;
}
this.bigdisplay = computation;
this.operation = undefined;
this.smalldisplay = "";
}
updateDisplay() {
this.bigDisplayTextElement.innerText = this.bigdisplay;
if (this.operation != null) {
this.smallDisplayTextElement.innerText = `${this.smalldisplay} ${this.operation}`;
} else if (this.operation == undefined) {
this.smallDisplayTextElement.innerText = this.smalldisplay;
}
}
updateMemo() {
let p = document.createElement("p");
let textP = document.createTextNode(this.bigdisplay);
p.appendChild(textP);
memo.appendChild(p);
console.log(this)
}
}
const numberButtons = document.querySelectorAll("[data-number]");
const operationButtons = document.querySelectorAll("[data-operation]");
const deleteButton = document.querySelector("[data-delete]");
const allClear = document.querySelector("[data-all-clear]");
const equalsButton = document.querySelector("[data-equals]");
const smallDisplayTextElement = document.querySelector("[data-small-display]");
const bigDisplayTextElement = document.querySelector("[data-big-display]");
const memo = document.getElementById("memo");
console.log(memo[0]);
const calculator = new Calculator(
smallDisplayTextElement,
bigDisplayTextElement
);
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
calculator.appendNumber(button.innerText);
calculator.updateDisplay();
});
});
operationButtons.forEach((button) => {
button.addEventListener("click", () => {
calculator.chooseOperation(button.innerText);
calculator.updateDisplay();
});
});
allClear.addEventListener("click", () => {
calculator.clear();
calculator.updateDisplay();
});
equalsButton.addEventListener("click", () => {
calculator.compute();
calculator.updateDisplay();
calculator.updateMemo();
});
deleteButton.addEventListener("click", () => {
calculator.deleteNumbers();
calculator.updateDisplay();
});