-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (98 loc) · 3.16 KB
/
app.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
const quizzes = [
{
question: 'Who was the first president of Nigeria?',
options: [
'President Muhammadu Buhari',
'President Olusegun Obasanjo',
'President Nnamdi Azikuwe',
'President Goodluck Jonathan'],
correct: 'President Nnamdi Azikuwe',
},
{
question: 'What is the color of the Nigerian flag?',
options: [
'Lilac, White, Blue',
'Blue, white, Red',
'Green, White, Green',
'Yellow, White, Blue'],
correct: 'Green, White, Green',
},
{
question: 'Who was the first woman to drive a car?',
options: [
'Mrs. Olufunmiloyo Ramsome Kuti',
'Mrs. Aisha Umar',
'Miss. Zainab Babayaro',
'Mrs. Patience Jonathan'],
correct: 'Miss. Zainab Babayaro',
},
{
question: 'What does the eagle in the Nigerian coat of arms represent?',
options: [
'Strength',
'Power',
'Corruption',
'Culture'],
correct: 'Strength',
},
{
question: 'A cancar worm that has eaten deep into Nigeria symbolizes?',
options: [
'Earthworm',
'Ringworm',
'Corruption',
'Way of life'],
correct: 'Corruption',
}
]
let question = document.querySelector(".question");
let options = document.querySelector('.answer-body > .options')
let quizHeader = document.querySelector(".quiz-header");
const answerBody = document.querySelector(".answer-body");
const mainContainer = document.querySelector(".main-container");
const submit = document.querySelector(".submit")
function changeQuiz(n) {
const currentQuestion = quizzes[n]
question.textContent = currentQuestion.question
options.innerHTML = ''
currentQuestion.options.forEach(option => {
options.innerHTML += `<li class="option-a">
<input type="radio" name="answer" onchange="selectAnswer(event)">
<label for="a">${option}</label>
</li>`
})
};
changeQuiz(0);
let noOfCorrectCounts = 0
const answer = []
let totalPercentage
let counter = 0
const submitAnswer = () => {
if (counter === quizzes.length - 1) {
computeTotalScore()
return
}
if (counter >= answer.length) {
alert('pick an option')
return
}
counter++
changeQuiz(counter)
}
const selectAnswer = (event) => {
const ans = event.target.nextElementSibling.textContent
answer.push(ans)
correctOpt(ans)
}
function correctOpt(expectedAns) {
if (expectedAns === quizzes[counter].correct) {
noOfCorrectCounts++
}
}
function computeTotalScore() {
totalPercentage = `This is the total score ${(noOfCorrectCounts / quizzes.length) * 100} % <br> You got ${noOfCorrectCounts} questions right <br> You failed ${quizzes.length - noOfCorrectCounts} `
document.getElementById('display-score').innerHTML = totalPercentage
document.getElementById("quiz-header").style.display = "none";
document.getElementById("options").style.display = "none";
document.getElementById("submit").style.display = "none";
}