-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
201 lines (169 loc) · 6.14 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const quizDB = [
{
question: "What is the full form of HTML?",
options: ["Hello To My Land", "Hey Text Markup Language", "HyperText Makeup Language", "Hypertext Markup Language"],
ans: 3
},
{
question: "What is the full form of css?",
options: ["Cascading Style Sheets", "Cascading Style Sheep", "Cartoon Style Sheets", "Cascading Super Sheets"],
ans: 0
},
{
question: "What is the full form of HTTP?",
options: ["Hypertext Transfer Product", "Hypertext Test Protocol", "Hey Transfer Protocol", "Hypertext Transfer Protocol"],
ans: 3
},
{
question: "2+2=?",
options: [5,4,3,2],
ans: 1
},
];
const questionNo = document.querySelector(".question-no");
const question = document.querySelector(".question");
const optionContainer = document.querySelector(".option-container");
const btn = document.querySelector(".btn");
const showscore = document.querySelector(".showscore");
const quizBox = document.querySelector(".quiz-box");
const inputArea = document.querySelector(".input-area");
const start = document.querySelector(".start");
const userBox = document.querySelector(".user");
var nameText;
let questionCounter = 0;
let currentQuestion;
let availableQuestion = [];
let availableOptions = [];
var sum=0;
// setting up the timer function
function startTimer(){
let date = new Date(new Date().setTime(0));
let time = date.getTime();
let sec = Math.floor((time%(100*60))/1000);
let min = Math.floor((time%(1000*60*60))/(1000*60));
let stop = 1;
//setting timer
let x = setInterval(() => {
if(sec < 59){
sec++;
}
else{
sec = 0;
min++;
if(min == stop && sec == 0){
clearInterval(x);
quizBox.classList.add("hide");
//creating element
const msg = document.createElement("h5");
msg.setAttribute("class","msg");
msg.innerHTML = `Times up! 👎`
showscore.appendChild(msg);
const scoreBtn = document.createElement("button");
scoreBtn.setAttribute("class", "btn scoreBtn");
scoreBtn.innerText = "show result";
showscore.appendChild(scoreBtn);
// creating click event
scoreBtn.addEventListener("click",()=>{
msg.classList.add("hide");
scoreBtn.classList.add("hide");
showResult();
});
}
else{
min;
}
}
let formattedsec = sec < 10 ? `0${sec}`:`${sec}`;
let formattedmin = min < 10 ? `0${min}`:`${min}`;
document.querySelector(".timer").innerHTML = `${formattedmin} : ${formattedsec}`;
console.log(formattedmin,formattedsec);
}, 1000);
}
const totlaQuestion = quizDB.length;
//loading quizdb into availableArray
function setAvailableQuestion(){
quizDB.forEach(function(value){
availableQuestion.push(value);
});
}
function getNewQuestion(){
//set the question no
questionNo. innerHTML = "Question " + (questionCounter + 1) + " of " + totlaQuestion;
//get random questions
const questionIndex = availableQuestion[Math.floor(Math.random() * availableQuestion.length)];
currentQuestion = questionIndex;
question.innerText = (questionCounter + 1) + ". " + currentQuestion.question;
const index1 = availableQuestion.indexOf(questionIndex);
availableQuestion.splice(index1,1);
//setting options
const optionLen = currentQuestion.options.length;
//push options into availableoptions array
for(let i=0; i<optionLen;i++){
availableOptions.push(i);
}
optionContainer.innerHTML = " ";
for(let i=0; i<optionLen;i++) {
//random options
const optionIndex = availableOptions[Math.floor(Math.random() * availableOptions.length)];
const index2 = availableOptions.indexOf(optionIndex);
// splice it so the same option does not repeat
availableOptions.splice(index2,1);
//creating a div to showcase in front
const op = document.createElement("div");
op.innerHTML = currentQuestion.options[optionIndex];
op.id = optionIndex;
optionLen.className = "option";
optionContainer.appendChild(op);
// initializing click
op.setAttribute("onclick","getResult(this)");
}
questionCounter++;
}
function getResult(element){
const id = parseInt(element.id);
if(id === currentQuestion.ans){
console.log("correct");
sum++;
element.classList.add("correct");
}
else{
console.log("wrong");
element.classList.add("wrong");
}
// once user select one element they cannot deselct it or select another option
unclickableOption();
}
function unclickableOption(){
const optionLen = optionContainer.children.length;
for(let i=0; i<optionLen;i++){
optionContainer.children[i].classList.add("alreadyAnswered");
}
}
btn.addEventListener("click", function(){
if(questionCounter === quizDB.length){
console.log(sum);
quizBox.classList.add("hide");
showResult();
console.log(nameText);
}
else{
getNewQuestion();
}
});
function showResult(){
const result = document.createElement("h4");
result.setAttribute("class", "result");
result.innerHTML = `Congratulations! <br> ${nameText} You have scored: ${sum} / ${totlaQuestion}`;
showscore.appendChild(result);
}
//get the user name to print
start.addEventListener("click", function(){
nameText = inputArea.value;
userBox.classList.add("hide");
quizBox.classList.remove("hide");
startTimer();
});
//first set all ques in availabequeation
setAvailableQuestion();
//get the question
getNewQuestion();