-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (143 loc) · 6.13 KB
/
index.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
let quizComponents = document.querySelector('.quiz-components')
let mainText = document.querySelector('.start-text')
quizComponents.style.display = "none"
let start = document.querySelector(".start")
start.addEventListener('click', () => {
quizComponents.style.display = "block";
start.style.display = "none";
mainText.style.display = "none";
})
class Question {
constructor(title, options, correctAnswerIndex) {
this.title = title;
this.options = options;
this.correctAnswerIndex = correctAnswerIndex;
}
isCorrect(userAnswer) {
return this.correctAnswerIndex === userAnswer;
}
getCorrectAnswer() {
return this.options[this.correctAnswerIndex]
}
render() {
// let root = document.getElementById("root");
return `<form>
<fieldset>
<legend>${this.title}</legend>
<div class="options">
${this.options.map(
(option, index) =>
`<input type="radio" class="radio-btn" id="quizQuestion"
name=${this.title} value=${index}>
<label for="quizQuestion">${option}</label>
`).join("")}
</div>
<div>
</div>
</fieldset>
</form>`
}
}
let questionOne = new Question("1. A bat and a ball cost $1.10 in total. The bat costs $1 more than the ball. How much does the ball cost?", ["10 cents", "5 cents", "15 cents", "20 cents"],
1);
let questionTwo = new Question("If it takes five machines five minutes to make five widgets, how long would it take 100 machines to make 100 widgets?", ["5 minutes", "100 minutes", "50 minutes", "150 minutes"],
0);
let questionThree = new Question("In a lake, there is a patch of lily pads. Every day, the patch doubles in size. If it takes 48 days for the patch to cover the entire lake, how long would it take for the patch to cover half of the lake?", ["18 days", "47 days", "24 days", "48 days"],
1);
console.log(questionOne)
console.log("isCorrect", questionOne.isCorrect("option2"))
console.log("isCorrect", questionOne.isCorrect("option1"))
console.log("getCorrectAnswer ", questionOne.getCorrectAnswer())
questionOne.render();
questionTwo.render();
let root = document.getElementById("root")
let nextElm = document.querySelector('.btn')
let notification = document.querySelector(".notification")
let restart = document.querySelector('.restart')
let finalScore = document.querySelector('.final-score')
class Quiz {
constructor (rootElm, nextElm, questions, finalScore) {
this.questions = questions;
this.rootElm = rootElm;
this.activeQuestionIndex = 0;
this.nextElm= nextElm;
this.score = 0;
this.handleFunctions = this.handleFunctions();
this.finalScore = finalScore
}
handleFunctions(){
this.nextElm.addEventListener("click",() => {
let radioBtn = document.querySelectorAll('.radio-btn')
let selectBtn = [...radioBtn].filter(elm => {
return elm.checked
})
console.log(selectBtn)
if (!selectBtn[0]) {
return notification.innerText = "Please select an option to continue"
}
let currentQuestion = this.questions[this.activeQuestionIndex]
console.log(selectBtn[0].value)
if (currentQuestion.isCorrect(+selectBtn[0].value)){
this.score++
}
this.nextQuestion(this.activeQuestionIndex);
})
this.createUI()
}
nextQuestion () {
notification.innerText = "";
// finalScore.innerText = "";
this.activeQuestionIndex++;
if (this.activeQuestionIndex > this.questions.length -1)
{
root.style.display = "none";
nextElm.style.display = "none";
document.querySelector('.final-score').innerHTML = `<h2 class="score-header">Your final score is ${this.score}!!<h2>
<h3 class="solution-header">Here's the solution:<h3>
<p class="solution-para">1. Say the ball costs X. Then the bat costs $1 more, so it is X + 1. So we have bat + ball = X + (X + 1) = 1.1 because together they cost $1.10. This means 2X + 1 = 1.1, then 2X = 0.1, so X = 0.05. This means the ball costs 5 cents and the bat costs $1.05.</p>
<p class="solution-para">2. If it takes 5 machines 5 minutes to make 5 widgets, then it takes 1 machine 5 minutes to make 1 widget (each machine is making a widget in 5 minutes). If we have 100 machines working together, then each can make a widget in 5 minutes. So there will be 100 widgets in 5 minutes.</p>
<p class="solution-para">3. Every day FORWARD the patch doubles in size. So every day BACKWARDS means the patch halves in size. So on day 47 the lake is half full.</p>
`
console.log(this.score);
restart.style.display = "block";
restart.innerText = "Restart"
restart.addEventListener('click', this.restartQuiz)
return;
}
this.createUI();
}
restartQuiz () {
let quizRestart = new Quiz (root, nextElm, [questionOne, questionTwo], finalScore);
quizRestart.createUI()
root.style.display = "block";
nextElm.style.display = "block"
finalScore.innerText = "";
restart.style.display = "none";
}
createUI() {
restart.style.display = "none";
this.rootElm.innerHTML = this.questions[this.activeQuestionIndex].render()
}
}
let quiz = new Quiz (root, nextElm, [questionOne, questionTwo, questionThree], finalScore);
quiz.createUI();
console.log(quiz);
//On click on next, check whether the selected answer index is true or not
//if true, this.score++
//change this is handlefunctions
// Prevent Reload
document.onkeydown = function(){
switch (event.keyCode){
case 116 : //F5 button
event.returnValue = false;
event.keyCode = 0;
return false;
case 82 : //R button
if (event.ctrlKey){
event.returnValue = false;
event.keyCode = 0;
return false;
}
}
}
// Mouse Effects