-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
127 lines (110 loc) · 2.71 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
const historyDisplay = document.querySelector(".history");
const resultDisplay = document.querySelector(".result");
const numberButtons = document.querySelectorAll(".number");
const operatorButtons = document.querySelectorAll(".operator");
const equalButton = document.querySelector(".equal");
const clearButton = document.querySelector(".clear");
const deleteButton = document.querySelector(".delete");
const signButton = document.querySelector(".sign");
const copyright = document.querySelector(".copyright");
let currentInput = "0";
let previousInput = "";
let operator = "";
function updateDisplay() {
resultDisplay.value = currentInput;
historyDisplay.value =
previousInput + (operator ? " " + operator + " " : "");
}
function clear() {
currentInput = "0";
previousInput = "";
operator = "";
updateDisplay();
}
function deleteLast() {
currentInput = currentInput.length > 1 ? currentInput.slice(0, -1) : "0";
updateDisplay();
}
function appendNumber(number) {
if (currentInput === "0") {
currentInput = number;
} else {
currentInput += number;
}
updateDisplay();
}
function appendDecimal() {
if (!currentInput.includes(".")) {
currentInput += ".";
}
updateDisplay();
}
function setOperator(op) {
if (previousInput && currentInput) {
calculate();
}
operator = op;
previousInput = currentInput;
currentInput = "0";
updateDisplay();
}
function calculate() {
let result = 0;
const prev = parseFloat(previousInput);
const curr = parseFloat(currentInput);
if (isNaN(prev) || isNaN(curr)) return;
switch (operator) {
case "+":
result = prev + curr;
break;
case "-":
result = prev - curr;
break;
case "x":
result = prev * curr;
break;
case "÷":
if (curr === 0) {
result = "Error";
} else {
result = prev / curr;
}
break;
default:
return;
}
currentInput = result.toString();
previousInput = "";
operator = "";
updateDisplay();
}
function changeSign() {
if (currentInput !== "0") {
currentInput = (parseFloat(currentInput) * -1).toString();
updateDisplay();
}
}
function displayCopyright() {
const date = new Date().getFullYear();
copyright.textContent = `Made by Chalex ${date}`;
}
numberButtons.forEach((button) => {
button.addEventListener("click", () => {
if (button.textContent === ".") {
appendDecimal();
} else {
appendNumber(button.textContent);
}
});
});
operatorButtons.forEach((button) => {
button.addEventListener("click", () => {
setOperator(button.textContent);
});
});
equalButton.addEventListener("click", calculate);
clearButton.addEventListener("click", clear);
deleteButton.addEventListener("click", deleteLast);
signButton.addEventListener("click", changeSign);
updateDisplay(); // Initial display update
displayCopyright();