-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
135 lines (115 loc) · 4.13 KB
/
scripts.js
File metadata and controls
135 lines (115 loc) · 4.13 KB
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
// Inputs
const quantityInput = document.querySelector("#quantity");
const minRangeInput = document.querySelector("#min");
const maxRangeInput = document.querySelector("#max");
const checkboxRepeatInput = document.querySelector("#allow-repeats");
// Resetando os inputs no reload
quantityInput.value = "";
minRangeInput.value = "";
maxRangeInput.value = "";
checkboxRepeatInput.checked = false;
// Formatando os inputs
quantityInput.oninput = handleInputChange;
minRangeInput.oninput = handleInputChange;
maxRangeInput.oninput = handleInputChange;
// Sorting Wrappers
const sortWrapper = document.querySelector("#sort-form");
const resultsWrapper = document.querySelector("#result-tab");
// Buttons
const sortButton = document.querySelector(".submit-button .rainbow button");
const redoButton = document.querySelector(".redo-button .rainbow button");
// Lista de resultados
const resultList = document.querySelector(".results");
// Span que muda por iteração
const iterationTurn = document.querySelector(".title .overline");
sortButton.addEventListener("click", function (event) {
event.preventDefault();
let allInputsFilled =
quantityInput.value.trim() !== "" &&
minRangeInput.value.trim() !== "" &&
maxRangeInput.value.trim() !== "";
let isMinLessThanMax = minRangeInput.value < maxRangeInput.value;
let isQuantityValid =
parseInt(quantityInput.value) > 0 && parseInt(quantityInput.value) <= 1000;
let isQuantityBiggerThanRange =
parseInt(quantityInput.value) >
parseInt(maxRangeInput.value) - parseInt(minRangeInput.value) + 1 &&
checkboxRepeatInput.checked; // se isso retornar true, significa que a quantidade é maior que o intervalo
// se a quantidade for maior que o intervalo, não faz sentido permitir repetições
switch (true) {
case !allInputsFilled:
alert("Por favor, preencha todos os campos.");
return;
case !isMinLessThanMax:
alert("O valor mínimo deve ser menor que o valor máximo.");
return;
case !isQuantityValid:
alert("A quantidade deve ser entre 1 e 1000.");
return;
case isQuantityBiggerThanRange:
alert(
"A quantidade não pode ser maior que o intervalo quando repetições não são permitidas."
);
return;
}
// If all validations pass, proceed with sorting
results = sortNumbers(
parseInt(quantityInput.value),
parseInt(minRangeInput.value),
parseInt(maxRangeInput.value),
checkboxRepeatInput.checked
);
resultList.innerHTML = ""; // Clear previous results
// swap screens
sortWrapper.classList.add("d-none");
resultsWrapper.classList.remove("d-none");
// start adding sorting results
results.forEach((result, index) => {
setTimeout(() => {
const resultItem = document.createElement("div");
const resultText = document.createElement("span");
resultItem.append(resultText);
resultItem.classList.add("result-item", "background-animation");
resultText.classList.add("text-animation");
resultText.textContent = result;
iterationTurn.textContent = `${index + 1}º RESULTADO`;
resultList.appendChild(resultItem);
}, index * 3000); // same time as the animation
});
});
redoButton.addEventListener("click", function (event) {
event.preventDefault();
quantityInput.value = "";
minRangeInput.value = "";
maxRangeInput.value = "";
checkboxRepeatInput.checked = false;
sortWrapper.classList.remove("d-none");
resultsWrapper.classList.add("d-none");
});
// Functions
function handleInputChange(event) {
let value = event.target.value.replace(/\D/g, "");
if (value.length > 4) {
value = value.slice(0, 4);
}
event.target.value = value;
}
function sortNumbers(quantity, min, max, notAllowRepeats) {
const results = [];
const usedNumbers = [];
for (let i = 0; i < quantity; i++) {
let randomNumber;
do {
randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
} while (
(notAllowRepeats && usedNumbers.includes(randomNumber)) ||
randomNumber < min ||
randomNumber > max
);
results.push(randomNumber);
if (notAllowRepeats) {
usedNumbers.push(randomNumber);
}
}
return results;
}