-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
157 lines (143 loc) · 4 KB
/
scripts.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Global variables: displayed values, operands and operators
const history = document.querySelector("#history");
const displayNode = document.querySelector("#calc-display");
const decimalNode = document.querySelector("#decimal-btn");
let displayNum = "";
let numA, numB;
let operation;
// When await is true, entered numbers will overwrite the existing displayed number
let await = false;
// Number inputs
document.querySelectorAll(".num-btn").forEach((btn) => {
btn.id = `num-btn-${btn.dataset.num}`;
btn.addEventListener("click", () => {
// Add to display's number
setDisplay(btn.dataset.num, !await);
await = false;
});
});
// Decimal input
decimalNode.addEventListener("click", () => {
updateDecimalNode();
if (displayNode.textContent.includes(".")) {
return;
} else {
setDisplay(".", !await);
await = false;
return;
}
});
// Keyboard inputs
document.addEventListener("keydown", (e) => {
const key = e.key;
if (!isNaN(key)) {
document.querySelector(`#num-btn-${parseInt(key)}`).click();
} else if (operations.has(key)) {
document.querySelector(`#op-btn-${opToWord[key]}`).click();
} else if (e.ctrlKey && e.key === "Backspace") {
document.querySelector("#ce-btn").click();
} else {
switch (key) {
case ".":
document.querySelector(`#decimal-btn`).click();
break;
case "Enter":
e.preventDefault();
case "=":
document.querySelector(`#equals-btn`).click();
break;
case "Delete":
case "Backspace":
document.querySelector(`#del-btn`).click();
break;
case "Escape":
document.querySelector("#c-btn").click();
break;
}
}
});
// Arithmetic operations
document.querySelectorAll(".op-btn").forEach((btn) => {
btn.id = `op-btn-${opToWord[btn.dataset.op]}`;
btn.addEventListener("click", () => {
if (isValid(displayNum)) {
// Evaluate previous operation
if (operation) {
document.querySelector("#equals-btn").click();
// Error thrown "ERR", reset numbers
if (!isValid(displayNum)) {
history.textContent = "";
clearValues();
return;
}
}
// Set numA, clear upon entry of numB
numA = displayNum;
await = true;
operation = btn.dataset.op;
// Update history
if (numA !== null && numA !== undefined)
history.textContent = `${formatNum(numA)} ${operation}`;
}
updateDecimalNode();
});
});
// Evaluate expression
document.querySelector("#equals-btn").addEventListener("click", () => {
if (isValid(displayNum) && operation) {
calcEval();
// Update history
history.textContent = `${formatNum(numA)} ${operation} ${formatNum(
numB
)} = `;
clearValues();
}
});
// Clear entry
document.querySelector("#ce-btn").addEventListener("click", () => {
// Clear current display
setDisplay();
// Clear all history
if (await) {
[numA, numB] = [null, null];
operation = null;
history.textContent = "";
}
});
// Clear all
document.querySelector("#c-btn").addEventListener("click", () => {
// Start from scratch
setDisplay();
clearValues();
history.textContent = "";
});
// Delete last
document.querySelector("#del-btn").addEventListener("click", () => {
let txt = displayNode.textContent;
if (txt === "-" || txt === ".") {
setDisplay();
return;
}
if (txt.length > 0) {
let result = txt.match(/^[-]?[0-9]+[.]?[0-9]*/g);
if (result === null) {
return;
}
if (result[0] !== txt) {
return;
}
displayNode.textContent = displayNode.textContent.slice(0, -1);
displayNum = displayNode.textContent;
updateDecimalNode();
}
});
document.querySelector("#plus-minus-btn").addEventListener("click", () => {
if (displayNode.textContent[0] !== "-") {
displayNode.textContent = "-" + displayNode.textContent;
displayNum = "-" + displayNum;
} else {
displayNode.textContent = displayNode.textContent.slice(1);
displayNum = displayNum.slice(1);
}
await = false;
});