-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
150 lines (126 loc) · 3.87 KB
/
quiz.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
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
145
146
147
148
149
150
let startBtn=document.querySelector('.start-btn');
let nextBtn=document.querySelector('.next-btn');
let questionContainer=document.querySelector('.question')
let questionElement=document.querySelector('#question')
let answerContainer=document.querySelector('.answer')
let shuffleQuestions, currentQuestion;
startBtn.addEventListener('click', startGame);
nextBtn.addEventListener('click', ()=>{
currentQuestion++;
// added this to clear the next button
nextBtn.classList.add('question')
setNextQuestion();
});
//function to start the game
function startGame(){
startBtn.classList.add('question');
questionContainer.classList.remove('question');
// for shffling the questions
shuffleQuestions=questions.sort(()=> Math.random() - .5);
currentQuestion=0;
setNextQuestion()
}
// function to set the next question
function setNextQuestion(){
resetState()
showQuestion(shuffleQuestions[currentQuestion])
clearStatus(document.body)
}
//function to show questions
function showQuestion(question){
questionElement.textContent=question.question;
// loop through the answer
question.answer.forEach(answer=>{
const button=document.createElement('button');
button.textContent=answer.text;
button.classList.add('btn');
if(answer.correct){
button.dataset.correct= answer.correct;
}
button.addEventListener('click', selectAnswer);
answerContainer.appendChild(button);
})
}
//function to perform a task when an answer is click
function selectAnswer(e){
let selectedButton=e.target;
const correct=selectedButton.dataset.correct;
setStatus(document.body, correct)
// make the answer container an array
Array.from(answerContainer.children).forEach(button=>{
setStatus(button, button.dataset.correct)
})
// check if this is the last question
if(shuffleQuestions.length > currentQuestion + 1){
nextBtn.classList.remove('question');
}else{
startBtn.textContent='Restart';
startBtn.classList.remove('question')
}
}
// function to setStatus whether the answer is correct or not
function setStatus(element, correct){
clearStatus(element)
if(correct){
element.classList.add('correct')
}else{
element.classList.add('wrong')
}
}
// Function to clear the status
function clearStatus(element){
element.classList.remove('correct')
element.classList.remove('wrong')
}
//function to clear the previous question
function resetState(){
while(answerContainer.firstChild){
answerContainer.removeChild(answerContainer.firstChild)
}
}
// questions container
let questions=[
{
question:'Who program this?',
answer:[
{text:'Olaitan', correct: true},
{text:'Bisi', correct: false},
{text:'Babatunde', correct: true},
{text:'Bayo', correct: false},
]
},
{
question:'Who is the greatest rapper in Nigeria?',
answer:[
{text:'Olamide', correct: true},
{text:'Jude Okeye', correct: false},
{text:'M I', correct: false},
{text:'Ice prince', correct: false},
]
},
{
question:'What is the highest mountain?',
answer:[
{text:'Everest', correct: true},
{text:'Kilomanjaro', correct: false},
{text:'Spring', correct: false},
{text:'Waterfall', correct: false},
]
},
{
question:'Best learnning code website',
answer:[
{text:'MDN', correct: true},
{text:'w3school', correct: true}
]
},
{
question:'Who is the man',
answer:[
{text:'Bola', correct: true},
{text:'Bisi', correct: false},
{text:'Bami', correct: false},
{text:'Bayo', correct: false},
]
}
]