-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (104 loc) · 3.22 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
const LINKS = document.querySelectorAll("link");
const BUTTONS = document.querySelectorAll("input");
const PREV_OPERAND_TEXT = document.querySelector("[data-previous-operand]");
const CURRENT_OPERAND_TEXT = document.querySelector("[data-current-operand]");
const DELETE_BUTTON = document.querySelector("[data-delete]");
const RESULT_BUTTON = document.querySelector("[data-output]");
const RESET_BUTTON = document.querySelector("[data-reset]");
const OPERANDS = document.querySelectorAll("[data-num]");
const OPERATOR_BUTTON = document.querySelectorAll("[data-operator]");
let prevOperand = PREV_OPERAND_TEXT.innerText;
let currentOperand = CURRENT_OPERAND_TEXT.innerText;
let operation;
function cssThemeChange(i) { // Aquí tenemos que tener mucho cuidado con los Links, ya que el orden en el que están en el html, influye en la posición del array de Links en el que están
if (i === "1") {
LINKS[1].setAttribute("href", "");
} else {
// Esto funciona gracias a que html lee de arriba a abajo el código así que sobreescribe el enlace al css del theme1.css si el link que creamos es funcional
LINKS[1].setAttribute("href", `css/theme${i}.css`);
}
}
BUTTONS.forEach((btn) => {
btn.addEventListener("click", () => {
cssThemeChange(btn.value);
});
});
function reset() {
prevOperand = "";
currentOperand = "";
operation = undefined;
}
function displayNumScreen() {
CURRENT_OPERAND_TEXT.innerText = currentOperand.toLocaleString("en");
if (operation !== undefined) {
PREV_OPERAND_TEXT.innerText = `${prevOperand} ${operation.toString("en")}`;
} else {
PREV_OPERAND_TEXT.innerText = prevOperand;
}
}
RESET_BUTTON.addEventListener("click", () => {
reset();
displayNumScreen();
});
function deleteLastNumber() {
currentOperand = currentOperand.toString().slice(0, -1);
}
DELETE_BUTTON.addEventListener("click", () => {
deleteLastNumber();
displayNumScreen();
});
function addNumber(number) {
if (number === "." && currentOperand.includes(".")) return;
currentOperand = currentOperand.toString() + number.toString();
}
OPERANDS.forEach((operand) => {
operand.addEventListener("click", () => {
addNumber(operand.innerText);
displayNumScreen();
});
});
function calculateOperation() {
let result;
let prev = parseFloat(prevOperand);
let current = parseFloat(currentOperand);
if (isNaN(prev) || isNaN(current)) return;
switch (operation) {
case "+":
result = prev + current;
break;
case "-":
result = prev - current;
break;
case "×":
result = prev * current;
break;
case "/":
result = prev / current;
break;
default:
return;
}
currentOperand = result;
operation = undefined;
prevOperand = "";
PREV_OPERAND_TEXT.innerText = "";
}
RESULT_BUTTON.addEventListener("click", () => {
calculateOperation();
displayNumScreen();
});
function operationSelection(operate) {
if (CURRENT_OPERAND_TEXT === "") return;
if (PREV_OPERAND_TEXT !== "") {
calculateOperation();
}
operation = operate;
prevOperand = currentOperand;
currentOperand = "";
}
OPERATOR_BUTTON.forEach((btn) => {
btn.addEventListener("click", () => {
operationSelection(btn.innerText);
displayNumScreen();
});
});