-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
180 lines (169 loc) · 4.75 KB
/
main.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
let displayValue = 0; //am display angezeigt
let firOperand = 0; // nummer 1
let secOperand = 0; // nummer 2
let firOperator = ""; // operator 1
let result = 0;
let clicked = false; // disable decimal-button
let historyArr = [];
const add = (a, b) => a + b;
const substract = (a, b) => a - b;
const multiply = (a, b) => a * b;
const divide = (a, b) => {
if (b == 0) {
alert(">:(");
alert("not devidable by 0");
return 0;
} else {
return a / b;
}
};
const operate = (a, op, b) => {
if (op === "/") {
return divide(a, b);
} else if (op === "*") {
return multiply(a, b);
} else if (op === "+") {
return add(a, b);
} else if (op === "-") {
return substract(a, b);
}
};
// DOM
const calc = document.querySelector(".calc");
const display = document.querySelector(".displayVal");
const numbers = document.querySelectorAll(".num");
const deci = document.querySelector("#point");
const history = document.querySelector(".history");
window.addEventListener("keyup", (e) => {
if (e.key === "Shift") {
return;
} else if (e.key === "=") {
const key = document.querySelector(`button[data-key='Enter']`);
key.click();
} else {
const key = document.querySelector(`button[data-key='${e.key}']`);
key.click();
}
});
const showHistory = (num1, opr, num2) => {
if (opr === "/") {
opr = `÷`;
} else if (opr === "*") {
opr = "×";
}
if (historyArr.join(" ").length >= 83) {
while (historyArr.join(" ").length >= 45) {
historyArr.shift();
}
history.textContent = `${historyArr.join(" ")}`;
} else if (result === num1) {
historyArr.push(" ", opr, num2);
} else {
historyArr.push(num1, opr, num2);
}
history.textContent = `${historyArr.join(" ")}`;
};
const outputDisplay = () => (display.textContent = `${displayValue}`); //zeigt die Nummer am display an
const outputResult = () => (display.textContent = `${result}`); //zeigt Ergebnis an
const operatorDecision = (e) => {
if (e.target.getAttribute("id") === "divide") return "/";
else if (e.target.getAttribute("id") === "multiply") return "*";
else return `${e.target.innerText}`;
};
const giveResult = (num1, opr, num2) => {
showHistory(num1, opr, num2);
result = operate(Number(num1), opr, Number(num2));
if (result % 1 !== 0) {
result = parseFloat(result).toFixed(3);
}
outputResult();
firOperand = result;
secOperand = 0;
displayValue = 0;
};
const enableDecimals = () => {
displayValue = 0;
clicked = false;
};
const checkDecimal = () => {
if (displayValue % 1 !== 0) {
clicked = true;
} else {
clicked = false;
}
};
calc.addEventListener("click", (e) => {
if (e.target.hasAttribute("data-type")) {
if (displayValue == 0 && displayValue !== "0.") {
displayValue = `${e.target.innerText}`;
} else {
displayValue += `${e.target.innerText}`;
}
return outputDisplay();
} else if (e.target.hasAttribute("data-opr")) {
if (firOperand && firOperator !== "" && !secOperand && !displayValue) {
firOperator = operatorDecision(e);
}
if (!firOperator) {
if (!displayValue) firOperand = result;
else if (displayValue) firOperand = displayValue;
enableDecimals();
firOperator = operatorDecision(e);
} else if (firOperand && firOperator !== "") {
secOperand = displayValue;
enableDecimals();
giveResult(firOperand, firOperator, secOperand);
firOperator = operatorDecision(e);
} else if (!firOperand && firOperator && displayValue) {
firOperand = 0;
secOperand = displayValue;
enableDecimals();
giveResult(firOperand, firOperator, secOperand);
firOperator = operatorDecision(e);
}
} else if (e.target.getAttribute("id") === "vz") {
if (!displayValue) {
result *= -1;
outputResult();
} else {
displayValue *= -1;
outputDisplay();
}
} else if (e.target.getAttribute("id") === "percent") {
if (!displayValue) {
result *= 0.01;
outputResult();
} else {
displayValue *= 0.01;
outputDisplay();
}
}
if (e.target.getAttribute("id") === "point") {
checkDecimal();
if (!clicked) {
displayValue = `${displayValue}.`;
clicked = true;
outputDisplay();
} else return;
} else if (e.target.getAttribute("id") === "equals") {
if (!firOperator) {
enableDecimals();
return;
} else if (firOperator) {
if (!firOperand) firOperand = result;
secOperand = displayValue;
giveResult(firOperand, firOperator, secOperand);
}
firOperator = "";
} else if (e.target.getAttribute("id") === "clear") {
displayValue = 0;
secOperand = 0;
firOperand = 0;
firOperator = "";
historyArr = [];
history.textContent = "";
result = 0;
clicked = false;
outputDisplay();
}
});