-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
148 lines (133 loc) · 4.45 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
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
const questions = document.querySelector(".questions");
const optionlist = document.querySelector(".optionlist");
const option = document.querySelector(".option");
const timecnt = document.querySelector(".questiontime .time");
const nextbtn = document.querySelector(".nextbtn ");
const qindex = document.querySelector(".qindex");
const result = document.querySelector(".result");
const resulttext = document.querySelector(".result p");
const card = document.querySelector(".card");
const reply = document.querySelector(".reply");
const left = document.querySelector(".left");
reply.onclick = () => {
card.classList.remove('hide')
result.classList.remove('show')
questionCount = 0;
queslength = 1;
count;
timevalue = 15
score= 0
questioncounter(queslength);
showQuestion(questionCount);
clearInterval(count)
timecount(timevalue)
nextbtn.style.display = "none"
left.textContent = "time left"
}
nextbtn.onclick = () => {
if (questionCount < quizlist.length - 1) {
questionCount++;
queslength++;
questioncounter(queslength);
showQuestion(questionCount);
clearInterval(count)
timecount(timevalue)
nextbtn.style.display = "none"
left.textContent = "time left "
} else {
clearInterval(count)
showresutlbox()
}
};
let wrongcticon='<i class="fa-sharp fa-solid fa-xmark"></i>'
let correcticon ='<i class="fa-solid fa-check"></i>'
let questionCount = 0;
let queslength = 1;
let count;
let timevalue = 15
let score= 0
function showQuestion(index) {
let currentquestion = quizlist[questionCount];
questions.innerHTML = `${currentquestion.id}: ${currentquestion.question}`;
optionlist.innerHTML = "";
currentquestion.option.forEach((element) => {
optionlist.innerHTML += ` <div class="option">
<span>${element}</span>
</div>`;
});
const optionbtn = document.querySelectorAll(".option");
optionbtn.forEach((element) => {
element.setAttribute("onclick", "optionselected(this)");
});
}
showQuestion(questionCount);
function questioncounter(index) {
qindex.innerHTML = `${index} <span>of</span> ${quizlist.length} `;
}
questioncounter(queslength);
function optionselected(answer) {
clearInterval(count)
let useranswer = answer.textContent.trim();
let correct = quizlist[questionCount].correctanswer.trim();
let alloptions = optionlist.children.length
if (useranswer === correct) {
score +=1
answer.classList.add("correct")
answer.insertAdjacentHTML("beforeend", correcticon)
} else {
answer.classList.add("incorrect")
answer.insertAdjacentHTML("beforeend", wrongcticon)
for(let i = 0; i <alloptions; i++){
if(optionlist.children[i].textContent.trim()==correct){
optionlist.children[i].setAttribute("class","option correct")
optionlist.children[i].insertAdjacentHTML("beforeend", correcticon)
}
}
}
for(let i = 0; i < alloptions; i++){
optionlist.children[i].classList.add("disabled")
}
nextbtn.style.display = "block"
}
timecount(15)
function timecount(time){
count=setInterval(timer,1000)
function timer(){
timecnt.textContent=time
time--
if(time<9){
let addzero=timecnt.textContent
timecnt.textContent="0"+addzero
}
if(time < 0){
clearInterval(count)
timecnt.textContent='00'
let correct = quizlist[questionCount].correctanswer.trim();
let alloptions = optionlist.children.length
for(let i = 0; i <alloptions; i++){
if(optionlist.children[i].textContent.trim()==correct){
optionlist.children[i].setAttribute("class","option correct")
optionlist.children[i].insertAdjacentHTML("beforeend", correcticon)
}
}
for(let i = 0; i < alloptions; i++){
optionlist.children[i].classList.add("disabled")
}
nextbtn.style.display = "block"
left.textContent = "time off"
}
}
}
function showresutlbox(){
card.classList.add('hide')
result.classList.add('show')
if(score >7){
resulttext.innerHTML=`you have completed this quiz <br>and concrats!🙌 you got ${score} out ${quizlist.length}`
}
else if(score >4){
resulttext.innerHTML=`you have completed this quiz <br>and nice👌 you got ${score} out ${quizlist.length}`
}
else{
resulttext.innerHTML=`you have completed this quiz <br>and sorry😛 you got ${score} out ${quizlist.length}`
}
}