-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubmit.js
92 lines (71 loc) · 2.92 KB
/
submit.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
const urls = [];
function submitFilters() {
const url = "https://opentdb.com/api.php?";
/* Get the number of questions */
const inputElement = document.getElementById('limit');
var questionNumber = inputElement.value;
/* Get the selected categories */
const categoryCheckboxes = document.querySelectorAll('#category input[type="checkbox"]');
const selectedCategories = [];
categoryCheckboxes.forEach(checkbox => {
if (checkbox.checked) {
selectedCategories.push(checkbox.value);
}
});
/* Get the selected difficulty */
const difficultyRadios = document.querySelectorAll('#difficulty input[type="radio"]');
var selectedDifficulty = null;
difficultyRadios.forEach(radio => {
if (radio.checked) {
selectedDifficulty = radio.value;
}
});
/* Get the selected question types */
const typeCheckboxes = document.querySelectorAll('#question-type input[type="checkbox"]');
const selectedTypes = [];
typeCheckboxes.forEach(checkbox => {
if (checkbox.checked) {
selectedTypes.push(checkbox.value);
}
});
/* Validate the number of questions */
if (questionNumber == null)
return;
if (questionNumber < 10)
questionNumber = 10;
if (questionNumber > 40)
questionNumber = 40;
/* Calculate the number of questions per category */
var questionsPerCategory;
var leftoverQuestions;
if (selectedCategories.length > 0) {
questionsPerCategory = Math.floor(questionNumber / selectedCategories.length);
leftoverQuestions = questionNumber % selectedCategories.length;
}
/* Create the URLs */
/* arranges the difficulty and type parameters */
var difficulty = '';
if (selectedDifficulty != null) {
difficulty = `&difficulty=${selectedDifficulty}`;
}
/* if both or neither types are selected, the type parameter is not added, since it will pick both for both cases */
var type = '';
if (selectedTypes.length == 1) {
type = `&type=${selectedTypes[0]}`;
}
/* if no categories are selected, the URL will have only the amount, difficulty and type parameters */
if (selectedCategories.length == 0) {
urls.push(`${url}amount=${questionNumber}${difficulty}${type}`);
}
else { /* if categories are selected, the URLs will each have their divided amount, difficulty, type and category parameters */
selectedCategories.forEach(category => {
if (category == selectedCategories[selectedCategories.length - 1]) {
questionsPerCategory += leftoverQuestions;
}
urls.push(`${url}amount=${questionsPerCategory}${difficulty}${type}&category=${category}`);
});
}
// Converter o vetor em JSON e salvá-lo na memória local
localStorage.setItem('urls-fetch', JSON.stringify(urls));
window.location.href = "quiz.html";
}