-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (118 loc) · 3.56 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
128
129
130
131
132
133
134
135
136
137
// CREATE A QUIZ CLASS
class Quiz {
constructor(questions) {
this.score = 0;
this.questions = questions;
this.questionIndex = 0;
}
getQuestionIndex() {
return this.questions[this.questionIndex];
}
guess(answer) {
if (this.getQuestionIndex().isCorrectAnswer(answer)) {
this.score++;
}
this.questionIndex++;
}
isEnded() {
return this.questionIndex === this.questions.length;
}
}
// Create a question Class
class Question {
constructor(text, choices, answer) {
this.text = text;
this.choices = choices;
this.answer = answer;
}
isCorrectAnswer(choice) {
return this.answer === choice;
}
}
// NOW DISPLAY THE QUESTIONS
function displayQuestion() {
if (quiz.isEnded()) {
showScores();
} else {
// show question
let questionElement = document.getElementById("question");
questionElement.innerHTML = quiz.getQuestionIndex().text;
// show options
let choices = quiz.getQuestionIndex().choices;
for (let i = 0; i < choices.length; i++) {
let choiceElement = document.getElementById("choice" + i);
choiceElement.innerHTML = choices[i];
guess("btn" + i, choices[i]);
}
showProgress();
}
};
// GUESS ANSWER
function guess(id, guess) {
let button = document.getElementById(id);
button.onclick = function() {
quiz.guess(guess);
displayQuestion();
}
};
// SHOW QUIZ PROGRESS
function showProgress() {
let currentQuestionNumber = quiz.questionIndex + 1;
let ProgressElement = document.getElementById("progress");
ProgressElement.innerHTML =
`Question ${currentQuestionNumber} of ${quiz.questions.length}`;
};
// SHOW SCORES
function showScores() {
let quizEndHTML =
`
<h1>Quiz Completed</h1>
<h2 id='score'> Your scored: ${quiz.score} of ${quiz.questions.length}</h2>
<div class="quiz-repeat">
<a href="index.html">Take Quiz Again</a>
</div>
`;
let quizElement = document.getElementById("quiz");
quizElement.innerHTML = quizEndHTML;
};
// create questions here
let questions = [
new Question(
"Hyper Text Markup Language Stands For?", ["JQuery", "XHTML", "CSS", "HTML"], "HTML"
),
new Question(
"Cascading Style sheet stands for?", ["HTML", "JQuery", "CSS", "XML"], "CSS"
),
new Question(
"Which is a JavaScript Framework?", ["React", "Laravel", "Django", "Sass"], "React"
),
new Question(
"Which is a backend language?", ["PHP", "HTML", "React", "All"], "PHP"
),
new Question(
"Which is best for Artificial intelligence?", ["React", "Laravel", "Python", "Sass"], "Python"
)
];
// INITIALIZE quiz
let quiz = new Quiz(questions);
// display questions
displayQuestion();
// Add A CountDown for the Quiz
let time = 10;
let quizTimeInMinutes = time * 60 * 60;
let quizTime = quizTimeInMinutes / 60;
let counting = document.getElementById("count-down");
function startCountdown() {
let quizTimer = setInterval(function() {
if (quizTime <= 0) {
clearInterval(quizTimer);
showScores();
} else {
quizTime--;
let sec = Math.floor(quizTime % 60);
let min = Math.floor(quizTime / 60) % 60;
counting.innerHTML = `TIME: ${min} : ${sec}`;
}
}, 1000);
}
startCountdown();