-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (101 loc) · 3.07 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
118
119
120
121
122
123
124
125
126
127
const questions = {
"question1": {
"q": "Nothing Here",
"o0": {
"value": "Ans 1",
"isCorrect": false
},
"o1": {
"value": "Ans 2",
"isCorrect": false
},
"o2": {
"value": "Ans 3",
"isCorrect": true
},
"o3": {
"value": "Ans 4",
"isCorrect": false
}
}
}
const options = document.querySelectorAll('#opt')
const question = document.getElementById('question')
const optionBox = document.querySelectorAll('#option-box')
const scoreTxt = document.getElementById('score')
const qno = document.getElementById('qno')
const progress = document.querySelector('.progress-bar')
const container = document.querySelector('.container')
const finalContainer = document.querySelector('.final-container')
const restart = document.querySelector('.restart')
const finalScoreTxt = document.getElementById('final')
let currentQuestion = 1
let score = 0
let maxQuestion = 3
function assignQuestion(){
let questionNo = 'question' + currentQuestion
question.innerText = questions[questionNo].q
return questionNo
}
function assignOption(questionNo){
for(let i=0; i<4; i++){
let optNo = `o${i}`
options[i].innerText = questions[questionNo][optNo].value
}
}
function crtOption(questionNo){
for(let i=0; i<4; i++){
let optNo = `o${i}`
options[i].innerText = questions[questionNo][optNo].value
if(options[i].innerText = questions[questionNo][optNo].isCorrect){
return optNo
}
}
}
function nextWave(){
if(currentQuestion > maxQuestion) {
container.style.display = 'none'
finalContainer.style.display = 'block'
finalScoreTxt.innerText = score.toString()
}
if(currentQuestion <= maxQuestion) {
assignQuestion()
}
let questionNo = 'question' + currentQuestion
let crtNo = crtOption(questionNo)
assignOption(questionNo)
currentQuestion++
return crtNo
}
restart.addEventListener('click', () => {
currentQuestion = 1;
score = 0;
// Reset UI elements
container.style.display = 'block';
finalContainer.style.display = 'none';
scoreTxt.innerText = score.toString();
qno.innerText = `0/${maxQuestion}`;
progress.style.width = '0px';
crtNo = nextWave();
});
let crtNo = nextWave()
optionBox.forEach((opt)=>{
opt.addEventListener('click', ()=>{
if(opt.className == crtNo){
opt.style.backgroundColor = 'green'
score += 10
scoreTxt.innerText = score.toString()
qno.innerText = `${currentQuestion-1}/${maxQuestion}`
progress.style.width = `${(currentQuestion-1) * 30}px`
}
else{
opt.style.backgroundColor = 'red'
qno.innerText = `${currentQuestion-1}/${maxQuestion}`
progress.style.width = `${(currentQuestion-1) * 30}px`
}
setTimeout(()=>{
opt.style.backgroundColor = 'antiquewhite'
crtNo = nextWave()
}, 1000)
})
})