-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (172 loc) · 5.69 KB
/
script.js
File metadata and controls
189 lines (172 loc) · 5.69 KB
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
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');
// choices of question and answer
const quiz = [
{
question: "Q. The “Father of Artificial Intelligence” is:?",
choices: ["Alan Turing", "Charles babbage", "john McCarthy", "None of the above"],
answer: "john McCarthy"
},
{
question: "Q. Blind Search can be used for which of the following situations?",
choices: ["Real-Life Simulation", "Small search Space", "Advance Game Theory", "None of the above"],
answer: "Small search Space"
},
{
question: "Q.In how many category processes is Artificial Intelligence classified in?",
choices: ["Depends on the input nature", "5", "2", "3"],
answer: "3"
},
{
question: "Q.The proposition symbols in AI are?",
choices: ["True and False", "True,False and Null", "True.", " False."],
answer: "True and False"
},
{ question: "Q.Which of the following is a type of artificial intelligence agent?",
choices:["Learning AI Agent","Simple Reflex AI Agent.", "Goal-Based AI Agent", "All of the above"],
answer: "All of the above"
}
];
// 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 = 10;
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 = 10;
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 = 10;
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();
}
});
// This Code is given By Alok Kumar-->