-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
142 lines (122 loc) · 3.61 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
const numberBtns = document.querySelectorAll(".number");
const operatorBtns = document.querySelectorAll(".operator");
const resultDisplay = document.querySelector("#result");
const prevOpsDisplay = document.querySelector("#previous-operations");
const clearBtn = document.querySelector("#clear-button");
const equalBtn = document.querySelector("#equal");
const backspaceBtn = document.querySelector("#backspace-button");
let expression = [];
let currNum = "";
function evaluateExpr() {
if (expression.length < 3) return;
const [operand1, operator, operand2] = expression;
if (isNaN(operand1) || isNaN(operand2)) return;
let result;
switch (operator) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "×":
result = operand1 * operand2;
break;
case "÷":
result = operand1 / operand2;
break;
}
expression = [result];
resultDisplay.innerText = result;
}
function handleNumClick(event) {
const num = event.target.innerText;
appendNum(num);
}
function handleOpClick(event) {
const operator = event.target.innerText;
handleOp(operator);
}
function handleClearClick() {
currNum = "";
expression = [];
resultDisplay.innerText = "";
prevOpsDisplay.innerText = "";
}
function handleEqualClick() {
if (currNum) expression.push(parseFloat(currNum));
evaluateExpr();
prevOpsDisplay.innerText = resultDisplay.innerText;
currNum = "";
}
function handleBackspaceClick() {
if (!currNum) {
removeLastExpr();
return;
}
currNum = currNum.slice(0, -1);
resultDisplay.innerText = currNum;
}
function appendNum(num) {
currNum += num;
resultDisplay.innerText = currNum;
}
function handleOp(operator) {
if (currNum) expression.push(parseFloat(currNum));
else if (
expression[expression.length - 1] === "+" ||
expression[expression.length - 1] === "-" ||
expression[expression.length - 1] === "×" ||
expression[expression.length - 1] === "÷" ||
expression.length === 0
)
return;
expression.push(operator);
currNum = "";
resultDisplay.innerText = "";
evaluateExpr();
if (typeof expression[expression.length - 1] == "number")
expression.push(operator);
prevOpsDisplay.innerText = `${expression[0]} ${operator}`;
}
function removeLastExpr() {
if (expression.length === 0) return;
let prev = expression[expression.length - 1];
expression.pop();
if (prev === "+" || prev === "-" || prev === "×" || prev === "÷")
prevOpsDisplay.innerText = prevOpsDisplay.innerText.slice(0, -2);
else {
prevOpsDisplay.innerText = prevOpsDisplay.innerText.slice(0, -1);
if (Math.abs(prev) > 10) expression.push(Math.floor(prev / 10));
else
(prevOpsDisplay.innerText = ""),
(expression = []),
(currNum = ""),
(resultDisplay.innerText = "");
}
}
numberBtns.forEach((btn) => {
btn.addEventListener("click", handleNumClick);
});
operatorBtns.forEach((btn) => {
btn.addEventListener("click", handleOpClick);
});
clearBtn.addEventListener("click", handleClearClick);
equalBtn.addEventListener("click", handleEqualClick);
backspaceBtn.addEventListener("click", handleBackspaceClick);
document.addEventListener("keydown", handleKeyPress);
function handleKeyPress(e) {
const key = e.key;
if (!isNaN(key)) {
appendNum(key);
} else if (["+", "-", "*", "/"].includes(key)) {
const op = key === "*" ? "×" : key === "/" ? "÷" : key;
handleOp(op);
} else if (key === "Enter") {
handleEqualClick();
} else if (key === "Backspace") {
handleBackspaceClick();
} else if (key === "Escape") {
handleClearClick();
}
}