-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (111 loc) · 4.11 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
//kalkulator
document.addEventListener("DOMContentLoaded", function () {
const display = document.getElementById("liczba");
const previousDisplay = document.getElementById("previous");
let currentInput = "";
let operator = "";
let previousInput = "";
function updateDisplay(value) {
display.value = value;
}
function updatePreviousDisplay(value) {
previousDisplay.value = value;
}
function handleInput(value) {
if (!isNaN(value) || value === ",") {
if (value === ",") {
value = ".";
}
if (value === "." && !currentInput.includes(".")) {
currentInput += ".";
} else if (!isNaN(value)) {
currentInput += value;
}
updateDisplay(currentInput);
} else if (["+", "-", "×", "÷"].includes(value)) {
if (currentInput !== "") {
previousInput = currentInput;
updatePreviousDisplay(previousInput + " " + value);
currentInput = "";
operator = value;
updateDisplay("");
}
} else if (value === "Enter" || value === "=") {
if (previousInput !== "" && currentInput !== "") {
let num1 = parseFloat(previousInput);
let num2 = parseFloat(currentInput);
let result;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "×":
result = num1 * num2;
break;
case "÷":
result = num2 !== 0 ? num1 / num2 : "Błąd";
break;
}
updateDisplay(result);
updatePreviousDisplay("");
currentInput = result.toString();
previousInput = "";
operator = "";
}
} else if (value === "Backspace" || value === "⌫") {
currentInput = currentInput.slice(0, -1);
updateDisplay(currentInput);
} else if (value === "%") {
if (currentInput !== "") {
currentInput = (parseFloat(currentInput) / 100).toString();
updateDisplay(currentInput);
}
} else if (value === "1/x") {
if (currentInput !== "" && parseFloat(currentInput) !== 0) {
currentInput = (1 / parseFloat(currentInput)).toString();
updateDisplay(currentInput);
}
} else if (value === "√") {
if (currentInput !== "" && parseFloat(currentInput) >= 0) {
currentInput = Math.sqrt(parseFloat(currentInput)).toString();
updateDisplay(currentInput);
}
} else if (value === "x²") {
if (currentInput !== "") {
currentInput = Math.pow(parseFloat(currentInput), 2).toString();
updateDisplay(currentInput);
}
} else if (value === "±") {
if (currentInput !== "") {
currentInput = (-parseFloat(currentInput)).toString();
updateDisplay(currentInput);
}
} else if (value === "Escape" || value === "C" || value === "CE") {
currentInput = "";
if (value === "C") {
previousInput = "";
operator = "";
updatePreviousDisplay("");
}
updateDisplay("");
}
}
document.querySelectorAll(".cyfry, td button").forEach(button => {
button.addEventListener("click", function () {
handleInput(this.textContent);
});
});
document.addEventListener("keydown", function (event) {
let keyMap = {
"*": "×",
"/": "÷",
"Backspace": "⌫",
"Enter": "="
};
let key = keyMap[event.key] || event.key;
handleInput(key);
});
});