-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
185 lines (167 loc) · 5.61 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
const container = document.querySelector('.container');
const questionBox = document.querySelector('.question');
const choicesBox = document.querySelector('.choices');
const nextBtn = document.querySelector('.nextBtn');
const scoreCard = document.querySelector('.scoreCard');
const alert = document.querySelector('.alert');
const startBtn = document.querySelector('.startBtn');
const timer = document.querySelector('.timer');
// Make an array of objects that stores question, choices of question and answer
const quiz = [
{
question: "Q. Which of the following is not a CSS box model property?",
choices: ["margin", "padding", "border-radius", "border-collapse"],
answer: "border-collapse"
},
{
question: "Q. Which of the following is not a valid way to declare a function in JavaScript?",
choices: ["function myFunction() {}", " let myFunction = function() {};", "myFunction: function() {}", "const myFunction = () => {};"],
answer: "myFunction: function() {}"
},
{
question: "Q. Which of the following is not a JavaScript data type?",
choices: ["string", "boolean", "object", "float"],
answer: "float"
},
{
question: "Q. What is the purpose of the this keyword in JavaScript?",
choices: ["It refers to the current function.", "It refers to the current object.", "It refers to the parent object.", " It is used for comments."],
answer: "It refers to the current object."
}
];
// Making Variables
let currentQuestionIndex = 0;
let score = 0;
let quizOver = false;
let timeLeft = 15;
let timerID = null;
// Arrow Function to Show Questions
const showQuestions = () => {
const questionDetails = quiz[currentQuestionIndex];
questionBox.textContent = questionDetails.question;
choicesBox.textContent = "";
for (let i = 0; i < questionDetails.choices.length; i++) {
const currentChoice = questionDetails.choices[i];
const choiceDiv = document.createElement('div');
choiceDiv.textContent = currentChoice;
choiceDiv.classList.add('choice');
choicesBox.appendChild(choiceDiv);
choiceDiv.addEventListener('click', () => {
if (choiceDiv.classList.contains('selected')) {
choiceDiv.classList.remove('selected');
}
else {
choiceDiv.classList.add('selected');
}
});
}
if(currentQuestionIndex < quiz.length){
startTimer();
}
}
// Function to check answers
const checkAnswer = () => {
const selectedChoice = document.querySelector('.choice.selected');
if (selectedChoice.textContent === quiz[currentQuestionIndex].answer) {
// alert("Correct Answer!");
displayAlert("Correct Answer!");
score++;
}
else {
// alert("Wrong answer");
displayAlert(`Wrong Answer! ${quiz[currentQuestionIndex].answer} is the Correct Answer`);
}
timeLeft = 15;
currentQuestionIndex++;
if (currentQuestionIndex < quiz.length) {
showQuestions();
}
else {
stopTimer();
showScore();
}
}
// Function to show score
const showScore = () => {
questionBox.textContent = "";
choicesBox.textContent = "";
scoreCard.textContent = `You Scored ${score} out of ${quiz.length}!`;
displayAlert("You have completed this quiz!");
nextBtn.textContent = "Play Again";
quizOver = true;
timer.style.display = "none";
}
// Function to Show Alert
const displayAlert = (msg) => {
alert.style.display = "block";
alert.textContent = msg;
setTimeout(()=>{
alert.style.display = "none";
}, 2000);
}
// Function to Start Timer
const startTimer = () => {
clearInterval(timerID); // Check for any exist timers
timer.textContent = timeLeft;
const countDown = ()=>{
timeLeft--;
timer.textContent = timeLeft;
if(timeLeft === 0){
const confirmUser = confirm("Time Up!!! Do you want to play the quiz again");
if(confirmUser){
timeLeft = 15;
startQuiz();
}
else{
startBtn.style.display = "block";
container.style.display = "none";
return;
}
}
}
timerID = setInterval(countDown, 1000);
}
// Function to Stop Timer
const stopTimer = () =>{
clearInterval(timerID);
}
// Function to shuffle question
const shuffleQuestions = () =>{
for(let i=quiz.length-1; i>0; i--){
const j = Math.floor(Math.random() * (i+1));
[quiz[i], quiz[j]] = [quiz[j], quiz[i]];
}
currentQuestionIndex = 0;
showQuestions();
}
// Function to Start Quiz
const startQuiz = () =>{
timeLeft = 15;
timer.style.display = "flex";
shuffleQuestions();
}
// Adding Event Listener to Start Button
startBtn.addEventListener('click', ()=>{
startBtn.style.display = "none";
container.style.display = "block";
startQuiz();
});
nextBtn.addEventListener('click', () => {
const selectedChoice = document.querySelector('.choice.selected');
if (!selectedChoice && nextBtn.textContent === "Next") {
// alert("Select your answer");
displayAlert("Select your answer");
return;
}
if (quizOver) {
nextBtn.textContent = "Next";
scoreCard.textContent = "";
currentQuestionIndex = 0;
quizOver = false;
score = 0;
startQuiz();
}
else {
checkAnswer();
}
});