-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
307 lines (292 loc) · 10 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// >>>> Questions <<<<
var questions = [
{
question: "What is a JavaScript constructor function used for?",
option1: "Defining CSS styles for elements.",
option2: "Creating and initializing objects.",
option3: "Managing database connections.",
option4: "Controlling user interface animations.",
answer: "Creating and initializing objects.",
},
{
question:
"Which keyword is used to create a new instance of an object in JavaScript?",
option1: "this",
option2: "new",
option3: "create",
option4: "instance",
answer: "new",
},
{
question:
"What does the 'this' keyword refer to in a constructor function?",
option1: "The global object (e.g., 'window' in a browser).",
option2: "The prototype object of the constructor.",
option3: "The current instance of the object being created.",
option4: "A reserved keyword with no specific meaning.",
answer: "The current instance of the object being created.",
},
{
question:
"What is the purpose of the 'prototype' property in a constructor function?",
option1: "To define the constructor's name.",
option2: "To store private data for the object.",
option3: "To add methods and properties to all instances of the object.",
option4: "To prevent the object from being modified.",
answer: "To add methods and properties to all instances of the object.",
},
{
question:
"How do you access a property of an object created with a constructor function?",
option1: "Using the 'dot' notation (e.g., obj.property).",
option2: "By calling a separate function with the property name.",
option3: "With the 'for...in' loop to iterate through properties.",
option4: "By using the 'prototype' property of the constructor.",
answer: "Using the 'dot' notation (e.g., obj.property).",
},
];
var startBtn = document.querySelector(".startBtn");
var login_form = document.querySelector(".login_form");
var emailPass = document.querySelector(".emailPass");
var animate = document.querySelector("#animate");
var hideBtn = document.querySelector(".hideBtn");
var passIcon = document.querySelector(".showPass");
var userEmail = document.getElementById("uEmail");
var emailIcon = document.querySelector(".emailIcon");
var userPass = document.getElementById("uPass");
var infoBox = document.querySelector(".info_box");
var quizStart = document.querySelector(".quiz_container");
var resultbox = document.querySelector(".result_box");
var scoreText = document.querySelector(".score_text");
var inputs = document.querySelector(".inputs");
var links = document.querySelector(".links");
var progressbar = document.getElementById("progressBar");
var form = document.getElementById("form");
var countDown = document.getElementById("timer");
var index = 0; // Question Counting
var score = 0; // User Correct Question Score
var counter; // counter of timer
var timeValue = 15; // timer value
// >>>> Redirection Login from to Registration form <<<<
var signUp = document.getElementById("signUp");
function singUp() {
window.location.href = "singUp.html";
}
// >>>> Registration form <<<<
var singUpName = document.getElementById("sName");
var singUpEmail = document.getElementById("sEmail");
var singUpPass = document.getElementById("sPass");
function submitForm() {
event.preventDefault();
var registerUser = {
name: singUpName.value,
email: singUpEmail.value,
password: singUpPass.value,
};
localStorage.setItem("registerUser", JSON.stringify(registerUser));
window.location.href = "./index.html";
}
// >>>> Get data from LocalStorage <<<<
var getUserData = localStorage.getItem("registerUser");
getUserData = JSON.parse(getUserData);
// >>>> Show localStorage Data <<<<
userEmail.addEventListener("click", () => {
emailIcon.style.display = "block";
animate.style.display = "block";
});
var remenberPass = document.getElementById("rePass");
var remenberEmail = document.getElementById("reEmail");
var loginTitle = document.querySelector(".title");
function showPopup() {
emailPass.style.display = "flex";
hideBtn.style.display = "none";
inputs.style.display = "none";
links.style.display = "none";
loginTitle.style.display = "none";
remenberEmail.textContent = getUserData.email;
remenberPass.textContent = getUserData.password;
}
function backToForm() {
inputs.style.display = "flex";
links.style.display = "flex";
emailPass.style.display = "none";
hideBtn.style.display = "block";
animate.style.color = "#007bff";
animate.classList.remove("animate__flash");
}
userPass.addEventListener("click", () => {
passIcon.style.display = "block";
});
// >>>> Login form <<<<
var getEmail = getUserData.email;
var getPass = getUserData.password;
function loginForm() {
var user = localStorage.getItem("user");
if (!user) {
const Toast = Swal.mixin({
toast: true,
position: "top",
showConfirmButton: true,
});
Toast.fire({
title: "<h2>Welcome!</h2> Please Register Your Account",
});
} else {
if (userEmail.value == getEmail && userPass.value == getPass) {
infoBox.style.display = "block";
login_form.style.display = "none";
const Toast = Swal.mixin({
toast: true,
position: "top",
showConfirmButton: false,
timer: 2000,
timerProgressBar: false,
didOpen: (toast) => {
toast.addEventListener("mouseenter", Swal.stopTimer);
toast.addEventListener("mouseleave", Swal.resumeTimer);
},
});
Toast.fire({
icon: "success",
title: "Signed in successfully",
});
} else {
const Toast = Swal.mixin({
toast: true,
position: "top",
showConfirmButton: false,
timer: 1000,
});
Toast.fire({
icon: "warning",
title: "<h2>Invalid</h2><h2>ID : Password</h2>",
});
setTimeout(function () {
animate.classList.add("animate__flash");
animate.style.color = "red";
}, 2000);
}
}
localStorage.setItem("user", true);
}
// Show Input Field Password
function showPass() {
if (userPass.type === "password") {
userPass.type = "text";
passIcon.innerHTML = "lock_open";
} else {
userPass.type = "password";
passIcon.innerHTML = "lock";
}
}
// >>>> Quit Quiz <<<<
function quit() {
location.reload();
}
// >>>> Enter Quiz <<<<
function enterQuiz() {
infoBox.style.display = "none";
startBtn.style.display = "block";
}
// >>>> Render Questions <<<<
function renderQuestions() {
var question = document.getElementById("qustionsContainer");
var options = document.getElementsByName("options");
var qustionNo = document.getElementById("qustionNo");
clearInterval(counter);
startTimer(timeValue);
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
if (options[i].value === questions[index - 1].answer) {
score++;
}
}
}
var percetage = (score / 5) * 100;
window.addEventListener("blur", () => {
clearInterval(counter);
resultbox.style.display = "flex";
quizStart.style.display = "none";
if (score === 0) {
scoreText.innerHTML = `
<span style="text-align: center; margin: 5px 0; font-size: 22px;">
You are not a child👶🏻, Don't cheat on exams💻. Shame on you!🖐️<span>
`;
} else {
var progressbar = document.querySelector(".progressBar");
if (progressbar) {
progressbar.innerHTML = `
<p>${percetage}%</p>
`;
}
scoreText.innerHTML = `
<span>Score: <p>${score}</p>out of<p>${options.length + 1}</p><span>
`;
}
});
if (!questions[index]) {
clearInterval(counter);
resultbox.style.display = "flex";
quizStart.style.display = "none";
scoreText.innerHTML = `
<span>Score: <p>${score}</p>out of<p>${options.length + 1}</p><span>
`;
var circularProgress = document.querySelector(".circular-progress"),
progressValue = document.querySelector(".progress-value");
var progressStartValue = 0,
progressEndValue = percetage,
speed = 25;
var progress = setInterval(() => {
progressStartValue++;
progressValue.textContent = `${progressStartValue}%`;
circularProgress.style.background = `conic-gradient(var(--primaryColor) ${
progressStartValue * 3.6
}deg, #ededed 0deg)`;
if (progressStartValue == progressEndValue) {
clearInterval(progress);
}
}, speed);
return;
}
var questionValue = questions[index];
question.innerHTML = `
<div id="qustions">
<span>1.</span>
<p>${questionValue.question}</p>
</div>
<div class="options_list">
<label for="options1" class="options"><input type="checkbox" id="options1" name="options" value="${questionValue.option1}">${questionValue.option1}<span class="checkmark"></span></label>
<label for="options2" class="options"><input type="checkbox" id="options2" name="options" value="${questionValue.option2}">${questionValue.option2}<span class="checkmark"></span></label>
<label for="options3" class="options"><input type="checkbox" id="options3" name="options" value="${questionValue.option3}">${questionValue.option3}<span class="checkmark"></span></label>
<label for="options4" class="options"><input type="checkbox" id="options4" name="options" value="${questionValue.option4}">${questionValue.option4}<span class="checkmark"></span></label>
</div>
`;
index++;
qustionNo.innerHTML = index;
}
// >>>> Timer <<<<
function startTimer(time) {
counter = setInterval(timer, 1000);
function timer() {
countDown.textContent = time;
time--;
if (time < 0) {
clearInterval(counter);
renderQuestions();
}
}
}
// >>>> Start Quiz <<<<
function startQuiz() {
quizStart.style.display = "block";
startBtn.style.display = "none";
renderQuestions();
document.documentElement.requestFullscreen();
}
// >>>> Restart Quiz <<<<
function restart() {
login_form.style.display = "none";
infoBox.style.display = "none";
startBtn.style.display = "block";
location.reload();
}