-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
158 lines (141 loc) · 4.02 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
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
158
const display = document.getElementById("display");
const buttons = Array.from(document.getElementsByClassName("btn"));
let expression = "";
let currentNumber = "";
let lastInputWasOperator = false;
let resultDisplayed = false;
let activeOperator = null;
function formatNumber(number) {
let [integer, decimal] = number.split(".");
integer = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ".");
return decimal ? `${integer},${decimal}` : integer;
}
function updateDisplay(value) {
if (value.length > 10) {
value = parseFloat(value).toExponential(5);
}
display.innerText = formatNumber(value.replace(".", ","));
}
function evaluateExpression() {
try {
const sanitizedExpression = expression.replace(/,/g, ".");
const result = Function(`"use strict"; return (${sanitizedExpression})`)();
return result.toString().replace(".", ",");
} catch {
return "Error";
}
}
function handleOperator(operator, buttonElement) {
if (currentNumber !== "") {
expression += currentNumber;
currentNumber = "";
}
if (!lastInputWasOperator) {
expression += operator;
} else {
expression = expression.slice(0, -1) + operator;
}
lastInputWasOperator = true;
if (activeOperator) {
activeOperator.classList.remove("active-operator");
}
buttonElement.classList.add("active-operator");
activeOperator = buttonElement;
let result = evaluateExpression();
if (result !== "Error") {
updateDisplay(result);
}
}
function handleEqual() {
if (currentNumber !== "") {
expression += currentNumber;
currentNumber = "";
}
let result = evaluateExpression();
if (result !== "Error") {
updateDisplay(result);
expression = result.replace(",", ".");
resultDisplayed = true;
} else {
display.innerText = "Error";
expression = "";
}
lastInputWasOperator = false;
if (activeOperator) {
activeOperator.classList.remove("active-operator");
activeOperator = null;
}
}
function handleBackspace() {
if (resultDisplayed) return;
if (currentNumber.length > 0) {
currentNumber = currentNumber.slice(0, -1);
updateDisplay(currentNumber || "0");
}
}
function handlePlusMinus() {
if (currentNumber) {
currentNumber = currentNumber.startsWith("-")
? currentNumber.slice(1)
: "-" + currentNumber;
updateDisplay(currentNumber);
}
}
function handlePercentage() {
if (currentNumber) {
let currentValue = parseFloat(currentNumber.replace(",", "."));
currentValue /= 100;
currentNumber = currentValue.toString().replace(".", ",");
updateDisplay(currentNumber);
}
}
function resetCalculator() {
display.innerText = "0";
expression = "";
currentNumber = "";
lastInputWasOperator = false;
resultDisplayed = false;
if (activeOperator) {
activeOperator.classList.remove("active-operator");
activeOperator = null;
}
}
buttons.map((button) => {
button.addEventListener("click", (e) => {
const value = e.target.innerText;
if (["+", "-", "*", "/"].includes(value)) {
handleOperator(value, e.target);
} else if (value === "=") {
handleEqual();
} else if (value === "AC") {
resetCalculator();
} else if (value === "←") {
handleBackspace();
} else if (value === "+/-") {
handlePlusMinus();
} else if (value === "%") {
handlePercentage();
lastInputWasOperator = false;
} else {
if (resultDisplayed) {
display.innerText = value === "," ? "0," : value;
currentNumber = display.innerText.replace(",", ".");
resultDisplayed = false;
} else {
if (currentNumber === "0" && value !== ",") {
currentNumber = value;
} else if (!(value === "," && currentNumber.includes(","))) {
currentNumber += value.replace(",", ".");
} else if (value === ",") {
currentNumber += ".";
}
updateDisplay(currentNumber);
}
lastInputWasOperator = false;
if (activeOperator) {
activeOperator.classList.remove("active-operator");
activeOperator = null;
}
}
});
});