-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz.html
144 lines (116 loc) · 4.78 KB
/
quiz.html
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
136
137
138
139
140
141
142
143
144
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="quiz.css">
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>Quiz</title>
</head>
<body onload="start()">
<div class="page">
<div class="progressbar"></div>
<div class="question"></div>
<div class="options">
<input type="radio" name="radioans" class="radioans">
<label class="ans"></label><br>
<input type="radio" name="radioans" class="radioans">
<label class="ans"></label><br>
<input type="radio" name="radioans" class="radioans">
<label class="ans"></label><br>
<input type="radio" name="radioans" class="radioans">
<label class="ans"></label><br>
</div>
<button class="submitbutton">Submit</button>
<div class="feedback"></div>
<div class="result">
<div class="dispscore"></div>
<button class="retry">Retry</button>
</div>
</div>
<script>
var current = 0;
var questions ;
var myinterval;
var sunmitted = 0;
var score = 0;
//start();
async function start(){
questions = await getquestions();
//var current = 0;
//setInterval(nextquestion,3000,)
displayquestion(questions,current);
starttimer(questions);
}
function displayquestion(ques, i){
console.log(ques);
let ind = Math.random()*4;
document.getElementsByClassName("feedback")[0].style.opacity = 0;
document.getElementsByClassName("question")[0].innerHTML = ques[i].question;
ques[i].incorrectAnswers.splice(ind,0,ques[i].correctAnswer)
const options = document.getElementsByClassName("ans");
for(let j = 0; j<4; j++){
options[j].innerHTML = ques[i].incorrectAnswers[j];
options[j].setAttribute("for",ques[i].incorrectAnswers[j])
document.getElementsByClassName("radioans")[j].setAttribute("value",ques[i].incorrectAnswers[j])
document.getElementsByClassName("radioans")[j].checked = false;
document.getElementsByClassName("radioans")[j].disabled = false;
}
// options[3].innerHTML = ques[i].correctAnswer;
// options[3].setAttribute("for",ques[i].correctAnswer)
// document.getElementsByClassName("radioans")[3].setAttribute("value",ques[i].correctAnswer)
// document.getElementsByClassName("radioans")[3].checked = false;
// document.getElementsByClassName("radioans")[3].disabled = false;
}
function starttimer(q){
console.log(q);
myinterval = setInterval(nextquestion, 7000,q);
}
function nextquestion(q){
console.log(q);
current++;
if(current == 9){
clearInterval(myinterval)
finish();
}
displayquestion(q,current)
}
async function getquestions(){
var arr = await axios.get("https://the-trivia-api.com/api/questions?limit=10");
//console.log(arr.data.incorrectAnswers)
//arr.data.incorrectAnswers = arr.data.incorrectAnswers.slice(0,ind).push(arr.data.correctAnswer).concat(ind+1);
//(arr.data.incorrectAnswers).splice(ind,0,arr.data.correctAnswer);
console.log(arr.data);
return arr.data;
}
function finish(){
document.getElementsByClassName("dispscore")[0].innerHTML = "Your score = "+score;
const res = document.getElementsByClassName("result")[0];
res.style.opacity = 1;
}
document.getElementsByClassName("submitbutton")[0].addEventListener("click",()=>{
const answ = document.getElementsByClassName("radioans");
const feedback = document.getElementsByClassName("feedback")[0];
let flag = 0;
for(let i = 0; i<4; i++){
if(answ[i].value == questions[current].correctAnswer && answ[i].checked == true){
flag = 1;
score+=10;
break;
}
answ[i].disabled = true;
}
feedback.style.opacity = 1;
if(flag == 0){
feedback.innerHTML = "wrong";
}
else{
feedback.innerHTML = "correct"
}
})
document.getElementsByClassName("retry")[0].addEventListener("click",function(){
location.reload();
})
</script>
</body>
</html>