-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (55 loc) · 2.44 KB
/
script.js
File metadata and controls
60 lines (55 loc) · 2.44 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
document.addEventListener('DOMContentLoaded', function () {
const raffleType = document.getElementById('raffle-type');
const numberFrame = document.getElementById('number-frame');
const nameFrame = document.getElementById('name-frame');
const fileInput = document.getElementById('file-input');
const uploadButton = document.getElementById('upload-button');
const fileLabel = document.getElementById('file-label');
const startButton = document.getElementById('start-button');
const resultLabel = document.getElementById('result-label');
const minNum = document.getElementById('min-num');
const maxNum = document.getElementById('max-num');
let namesList = [];
raffleType.addEventListener('change', function () {
if (raffleType.value === 'numbers') {
numberFrame.style.display = 'block';
nameFrame.style.display = 'none';
} else {
numberFrame.style.display = 'none';
nameFrame.style.display = 'block';
}
});
uploadButton.addEventListener('click', function () {
fileInput.click();
});
fileInput.addEventListener('change', function () {
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
namesList = e.target.result.split('\n').map(name => name.trim()).filter(name => name);
fileLabel.textContent = `Carregado ${namesList.length} nomes de ${file.name}`;
};
reader.readAsText(file);
}
});
startButton.addEventListener('click', function () {
if (raffleType.value === 'numbers') {
const min = parseInt(minNum.value);
const max = parseInt(maxNum.value);
if (isNaN(min) || isNaN(max)) {
resultLabel.textContent = 'Insira um número válido';
return;
}
const result = Math.floor(Math.random() * (max - min + 1)) + min;
resultLabel.textContent = `O número sorteado é: ${result}`;
} else {
if (namesList.length === 0) {
resultLabel.textContent = 'Insira lista de nomes primeiro';
return;
}
const winner = namesList[Math.floor(Math.random() * namesList.length)];
resultLabel.textContent = `Vencedor(a): ${winner}`;
}
});
});