-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (105 loc) · 2.86 KB
/
script.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 quizData = [
{
question: 'What is the capital of India?',
a: 'New Delhi',
b: 'Kolkata',
c: 'Bangalore',
d: 'Hyderabad',
correct: 'a'
}, {
question: 'What is the national sport of India?',
a: 'Cricket',
b: 'Hockey',
c: 'Football',
d: 'Kabaddi',
correct: 'b'
},{
question: 'What does HTML stands for',
a: 'Hello world',
b: 'HypoText Marking Language',
c: 'Bangalore',
d: 'HyperText Markup Language',
correct: 'd'
},{
question: 'What is the capital of India?',
a: 'New Delhi',
b: 'Kolkata',
c: 'Bangalore',
d: 'Hyderabad',
correct: 'a'
}, {
question: 'What is the national sport of India?',
a: 'Cricket',
b: 'Hockey',
c: 'Football',
d: 'Kabaddi',
correct: 'b'
},{
question: 'What does HTML stands for',
a: 'Hello world',
b: 'HypoText Marking Language',
c: 'Bangalore',
d: 'HyperText Markup Language',
correct: 'd'
}
]
const questionEl = document.getElementById("question");
const quiz = document.getElementById("quiz");
const aText = document.getElementById("a-text");
const bText = document.getElementById("b-text");
const cText = document.getElementById("c-text");
const dText = document.getElementById("d-text");
const buttonEl = document.getElementById("submit");
const answersEls = document.querySelectorAll(".answer");
let currentQuestion = 0;
let score = 0;
loadQuiz();
function loadQuiz(){
deselectAnswers();
questionEl.textContent = quizData[currentQuestion].question;
aText.textContent = quizData[currentQuestion].a;
bText.textContent = quizData[currentQuestion].b;
cText.textContent = quizData[currentQuestion].c;
dText.textContent = quizData[currentQuestion].d;
}
function getSelected(){
let answer = undefined;
answersEls.forEach((answerEl) => {
if(answerEl.checked){
answer = answerEl.id;
}
});
return answer;
}
function deselectAnswers(){
answersEls.forEach((answerEl) => {
if(answerEl.id === getSelected()){
answerEl.checked = false;
}
})
}
buttonEl.addEventListener("click", function() {
let answer = getSelected();
if(answer){
if(answer == quizData[currentQuestion].correct){
++score;
}
currentQuestion++;
if((currentQuestion < quizData.length)){
loadQuiz();
}
else{
quiz.innerHTML=`
<h2>
You've answered ${score} questions correctly
</h2>
<button onclick="location.reload()">
Reload
</button>
`
}
}
else{
alert("You've to choose one option!!");
}
})