-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuizApp.js
98 lines (92 loc) · 2.29 KB
/
QuizApp.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
const questions = [
{
'que':'which of the fallowing is markup language',
'a' :'CSS',
'b' :'php',
'c' :'Javascript',
'd' :'HTML',
'Correct':'a'
},
{
'que':'which of the fallowing is use for backend purpose',
'a' :'React',
'b' :'php',
'c' :'JS',
'd' :'scss',
'Correct':'b'
},
{
'que':'which of the fallowing is type of database',
'a' :'MYSql',
'b' :'php',
'c' :'mangotb',
'd' :'expresss',
'Correct':'a'
}
];
let index= 0;
let total=questions.length;
let right=0;
let wrong=0;
let questionhead= document.getElementById("question-heading")
//select all element by class name by .options
const optionInputs = document.querySelectorAll(".options");
const questiononload= () => {
//when question ends
if (index===total) {
return endQuiz()
}
//when new question is loaded it will clear the previous checked option
reset()
const data=questions[index];
// console.log(data);
/// it will add one in index and display before the question
questionhead.innerHTML=` ${index +1}) ${data.que}`;
optionInputs[0].nextElementSibling.innerHTML=data.a;
optionInputs[1].nextElementSibling.innerHTML=data.b;
optionInputs[2].nextElementSibling.innerHTML=data.c;
optionInputs[3].nextElementSibling.innerHTML=data.d;
}
const submitQuiz=()=>{
const data=questions[index];
const ans= getanser();
if (ans===data.Correct) {
right++;
}
else{
wrong++;
}
index++;
questiononload() ;
return;
}
const getanser=()=>{
let anser;
optionInputs.forEach(
(input)=>{
if (input.checked) {
// console.log(input.value);
anser= input.value;
// console.log('yes');
}
// else{
// console.log('no');
// }
}
)
return anser;
}
const reset=()=>{
optionInputs.forEach(
(input)=>{
input.checked=false;
}
)
}
const endQuiz= ()=>{
document.getElementById("box1").innerHTML=`
<h3> thankx for Quiz</h3>
<h2> ${right} / ${total} are correct ansers</h2>
`;
};
questiononload();