-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
130 lines (116 loc) · 3.29 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
const currentValue = document.querySelector(".calc-current-value");
const buttons = document.querySelectorAll(".calc-btn-container button");
const buttonsOperation = document.querySelectorAll(".calc-btn-operation");
const operationScheme = ["/", "x", "-", "+"];
const functionScheme = ["C", "+/-", "%"];
let operation = "";
let oldCurrentValue = 0;
let canNewCurrentValue = false;
function resetCalculator() {
oldCurrentValue = 0;
buttonsOperation.forEach(
(e) => e.innerHTML === operation && (e.classList = "calc-btn-operation")
);
operation = "";
}
function handleCurrentValue(value) {
if (canNewCurrentValue === true) {
currentValue.innerText = 0;
canNewCurrentValue = false;
}
if (value === "." && currentValue.innerText.includes(".")) {
return;
} else if (
value === "0" &&
currentValue.innerText[0] === 0 &&
currentValue.innerText.length === 1
) {
return;
} else if (currentValue.innerText.length > 9) {
return;
} else if (currentValue.innerText.length > 0) {
buttons.forEach((e) => e.innerText === "AC" && (e.innerText = "C"));
if (currentValue.innerText == 0) {
currentValue.innerText = value;
} else {
currentValue.innerText += value;
}
}
}
function handleFunctions(digit) {
switch (digit) {
case "C":
if (currentValue.innerText === 0 && operation !== "") {
resetCalculator();
}
currentValue.innerText = 0;
buttons.forEach((e) => (e.innerText === "C" ? (e.innerText = "AC") : "C"));
break;
case "+/-":
if (currentValue.innerText.startsWith("-")) {
currentValue.innerText = currentValue.innerText.replace("-", "");
} else {
currentValue.innerText = "-" + currentValue.innerText;
}
break;
case "%":
if (currentValue.innerText.length > 9) {
return;
}
currentValue.innerText = +currentValue.innerText / 100;
break;
default:
break;
}
}
function handleOperations(valueText) {
operation = valueText;
buttonsOperation.forEach((e) =>
e.innerHTML === operation
? (e.classList = "calc-btn-operation-selected")
: (e.classList = "calc-btn-operation")
);
canNewCurrentValue = true;
oldCurrentValue = currentValue.innerText;
}
function handleCalculus() {
let calculateNumber;
switch (operation) {
case "x":
calculateNumber = +oldCurrentValue * +currentValue.innerText;
break;
case "/":
calculateNumber = +oldCurrentValue / +currentValue.innerText;
break;
case "-":
calculateNumber = +oldCurrentValue - +currentValue.innerText;
break;
case "+":
calculateNumber = +oldCurrentValue + +currentValue.innerText;
break;
default:
return;
}
if (calculateNumber.length > 9) {
calculateNumber.toExponential();
}
currentValue.innerText = calculateNumber;
resetCalculator();
}
function handleEvent(event) {
const digit = event.target.innerText;
if (+digit >= 0 || digit === ".") {
handleCurrentValue(digit);
} else if (functionScheme.includes(digit)) {
handleFunctions(digit);
} else if (operationScheme.includes(digit)) {
handleOperations(digit);
} else if ("=" === digit) {
handleCalculus();
}
}
buttons.forEach((btn) => {
btn.addEventListener("click", (event) => {
handleEvent(event);
});
});