-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
290 lines (261 loc) · 9.6 KB
/
script.js
File metadata and controls
290 lines (261 loc) · 9.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
var highscoreDiv = document.getElementById("highscore");
var timerEl = document.getElementById("timer");
var sButton = document.getElementById("startbutton");
var conts = document.getElementsByClassName("container");
var questionList = document.querySelector("#questionList");
//var h2El = document.getElementById("question");
var resDiv = document.getElementById("resultDiv");
//var myForm = document.getElementById("myForm");
//var initial = document.getElementById("initials");
//var formH2 = h2El[0];
var listOfQuestions = [
{
question: "In HTML, DOM stands for ",
answers: ["1. Delivery On Monday", "2. Document Object Model", "3. Defined Object Memory", "4. None of the above"],
correctAnswer: 2
},
{
question: "HTML stands for?",
answers: ["1. Hyper Text Markup Language", "2. High Text Markup Language", "3. Hyper Tabular Markup Language", "4. None of the above"],
correctAnswer: 1
},
{
question: "Which of the following tag is used to mark a begining of paragraph?",
answers: ["1. <td>", "2. <div>", "3. <br>", "4. <p>"],
correctAnswer: 4
},
{
question: "Javascript is",
answers: ["1. A programming language", "2. Compiled code", "3. Scripting language", "4. None of the above"],
correctAnswer: 3
},
{
question: "Data types supported in JavaScript",
answers: ["1. Boolean", "2. String", "3. Number", "4. All the above"],
correctAnswer: 4
},
{
question: "Which of the following is true for Array in JavaScript?",
answers: ["1. It can contain only numbers", "2. It can contain only string", "3. A combination of different data type", "4. All the above"],
correctAnswer: 4
},
{
question: "window.setInterval() can take how many parameters?",
answers: ["1. One", "2. Two", "3. One or Two", "4. More than Two"],
correctAnswer: 2
},
{
question: "How to change the text color in CSS",
answers: ["1. font-color", "2. color", "3. backgroud-color", "4. None of the above"],
correctAnswer: 2
},
{
question: "pop in Array is used to",
answers: ["1. Removes from the beginning of an Array", "2. Removes from the End of an Array", "3. Removes from a specific Array index", "4. Allows you to programatically remove elements from an Array"],
correctAnswer: 1
},
{
question: "String in JavaScript is enclosed by",
answers: ["1. Single quotes", "2. Paranthesis", "3. Double quotes", "4. Sqaure brackets"],
correctAnswer: 3
},
{
question: "Which of the below tags in HTML do not have end tag",
answers: ["1. <br>", "2. <img>", "3. <input>", "4. All of the above"],
correctAnswer: 4
},
]
var cont1 = conts[0];
var cont2 = conts[1];
var cont3 = conts[2];
var val = timerEl.textContent;
console.log(val);
var questionCount = 0;
var secondsLeft = 100;
var lastScore = 0;
var allDone = false;
var eachQuesTimeLeft = 10;
var timerVariable = setTimeout(function(){
}, 10000);
var highscoreArray = [];
function init() {
console.log("Initialize");
timerEl.textContent = val + secondsLeft;
cont2.setAttribute("style", "display: none;");
cont3.setAttribute("style", "display: none;");
}
function startTimer() {
var timerInterval = setInterval(function() {
secondsLeft--;
timerEl.textContent = val + secondsLeft;
if(secondsLeft === 0 || allDone) {
clearInterval(timerInterval);
afterQuiz();
}
}, 1000);
}
sButton.addEventListener("click", function() {
event.preventDefault();
console.log("button clicked");
startTimer();
cont1.setAttribute("style", "display: none;");
startQuestions();
});
function startQuestions() {
console.log("In conatiner 2");
cont2.setAttribute("style", "display: block;");
populateQuestion();
}
// ****** For each question, add a timer
function populateQuestion() {
//setTimeout(function(){
if (questionCount < listOfQuestions.length) {
//h2El.textContent = listOfQuestions[questionCount].question;
questionList.textContent = listOfQuestions[questionCount].question;
console.log("question is: "+ questionList.textContent);
for (var i=0; i<4; i++) {
var lix = document.createElement("li");
var cButtonInfo;
lix.setAttribute("id", i);
cButtonInfo = listOfQuestions[questionCount].answers[i];
//console.log("From the list : " + listOfQuestions[questionCount].answers[i]);
//console.log("added to button : "+ cButtonInfo);
lix.innerHTML= "<button class=\"mcqButton\">" + cButtonInfo + "</button>";
//console.log("lix : " + lix.innerHTML);
questionList.appendChild(lix);
};
} else {
console.log("All questions are over!");
allDone = true;
}
timerVariable = setTimeout(function(){
console.log ("10 sec over !!!");
questionCount++;
clearTimeout(timerVariable); // clear the Question timeout
displayResult(0);
populateQuestion();
}, 10000);
};
function clearContainer() {
var element;
for (var i=0; i<4; i++) {
element = document.getElementById(i);
questionList.removeChild(element);
};
}
cont2.addEventListener("click", function() {
// Add - display "correct" if user is correct!
event.preventDefault();
if(event.target.matches("button")) {
var userAnswer = event.target.textContent;
console.log("user answer :"+userAnswer + " char at 0: "+userAnswer.charAt(0));
if (parseInt(userAnswer.charAt(0)) === listOfQuestions[questionCount].correctAnswer) {
//console.log("Hurray!!!!!");
lastScore++;
displayResult(1);
}
else {
displayResult(2);
//console.log(secondsLeft);
secondsLeft = secondsLeft-10;
//console.log(secondsLeft);
//console.log ("Next time!!!!");
}
questionCount++;
clearTimeout(timerVariable);
clearContainer();
populateQuestion();
};
});
function displayResult(num) {
if (num === 1) {
resDiv.innerHTML = "<hr/><p>Correct</p>";
} else if (num === 2) {
resDiv.innerHTML = "<hr><p>Wrong<p>";
} else {
resDiv.innerHTML = "<hr><p>Timed out<p>";
}
setTimeout(function(){
resDiv.innerHTML = "<p></p>";
//console.log("res " + resDiv.innerHTML);
},500);
};
function afterQuiz() {
//Add the information to populate High Score
console.log ("Time is up!");
cont2.setAttribute("style", "display: none;");
cont3.setAttribute("style", "display: block;");
var newH1 = document.createElement("h1");
newH1.textContent = "All Done!";
cont3.append (newH1);
var h3Element = document.createElement("h3");
//pElement.innerHTML = "<p>Your Score is: "+ lastScore + "</p>";
h3Element.textContent = "Your Score is: "+lastScore;
console.log ("what to display "+h3Element.textContent);
cont3.appendChild(h3Element);
var iLabel = document.createElement("label")
iLabel.setAttribute("style", "type: text, id: fname; name: initials;");
iLabel.textContent = "Enter your initials: ";
var inputBox = document.createElement("input");
inputBox.setAttribute("type", "text");
inputBox.setAttribute("id", "initials");
inputBox.setAttribute("name", "initials");
inputBox.setAttribute("placeholder", "abc");
//cont3.append("Enter your initials ", inputBox);
var smButton = document.createElement("button");
smButton.textContent = "Submit";
cont3.append (iLabel, inputBox);
cont3.append (smButton);
}
cont3.addEventListener("click", function() {
// clicking submit should take the user to a different html page
var initials = document.getElementById("initials");
event.preventDefault();
if(event.target.matches("button")) {
console.log("Clicked");
var highscoreObj = {
initial: initials.value.trim(),
score: lastScore
};
if (validateInitials(highscoreObj.initial)) {
var storedHighscore = JSON.parse(localStorage.getItem("highscoreArray"));
if(storedHighscore !== null) {
highscoreArray = storedHighscore;
console.log(storedHighscore);
}
highscoreArray.push(highscoreObj);
localStorage.setItem("highscoreArray", JSON.stringify(highscoreArray));
pageRedirect();
}
else {
alert ("Initials should be letters with length less than 5 characters");
}
};
});
function validateInitials(initalValue) {
var isValid = false;
if (initalValue === "" || initalValue.length > 5) {
return isValid;
} else if(initalValue.length >= 1 ) {
//var initialArray = initalValue.split('');
for (var i=0; i<initalValue.length; i++) {
if ( (initalValue.charCodeAt(i) > 64 && initalValue.charCodeAt(i) < 91) ||
(initalValue.charCodeAt(i) > 96 && initalValue.charCodeAt(i) < 123)) {
isValid = true;
} else {
isValid = false;
i = initalValue.length;
}
};
}
return isValid;
};
highscoreDiv.addEventListener("click", function() {
//event.preventDefault();
pageRedirect();
});
function pageRedirect() {
//window.location.replace("https://www.google.com/");
window.location.href = "highscore.html";
};
init();