-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (65 loc) · 2.67 KB
/
index.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
const form = document.getElementById('form');
const container = document.getElementById('container');
const questionform = document.getElementById('questionform');
let questions = [];
let question = 0;
let correct = 0;
let amount = "";
let difficulty = "";
let type = "";
let category = "";
form.addEventListener('submit', function(e) {
e.preventDefault();
amount = document.getElementById('amount').value;
difficulty = document.getElementById('difficulty').value;
type = document.getElementById('type').value;
category = document.getElementById('category').value;
fetch(`https://opentdb.com/api.php?amount=${amount}&difficulty=${difficulty}&type=${type}&category=${category}`)
.then(request => request.json())
.then(response => {
questions = response.results;
form.innerHTML = '';
console.log(questions);
render();
});
})
function render(){
if(question >= parseInt(amount)) {
questionform.innerHTML = `<p>Finished</p>
<p>Correct answers   ${correct} / ${amount}</p>
<button class="button" onclick="location.reload()">Try Again</button>`;
return;
}
questionform.innerHTML = '';
if(type == 'multiple'){
questions[question].incorrect_answers.splice(getrand(),0,questions[question].correct_answer)
questionform.innerHTML = `
<h2>Category ${questions[question].category}</h2>
<p>Question ${question} / ${amount}</p>
<p>${questions[question].question}</p>
<button class="button" onclick="clickme('${questions[question].incorrect_answers[0]}')">${questions[question].incorrect_answers[0]}</button><br>
<button class="button" onclick="clickme('${questions[question].incorrect_answers[1]}')">${questions[question].incorrect_answers[1]}</button><br>
<button class="button" onclick="clickme('${questions[question].incorrect_answers[2]}')">${questions[question].incorrect_answers[2]}</button><br>
<button class="button" onclick="clickme('${questions[question].incorrect_answers[3]}')">${questions[question].incorrect_answers[3]}</button><br>
`;
}else{
questionform.innerHTML = `
<p>${questions[question].question}</p>
<button class="button" onclick="clickme('True')">True</button><br>
<button class="button" onclick="clickme('False')">False</button><br>
`;
}
}
function getrand(){
const num = parseInt(Math.random()*4);
console.log(num);
return num;
}
function clickme(index){
console.log(index);
if(index == questions[question].correct_answer){
correct++;
}
question++;
render();
}