-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (93 loc) · 3.26 KB
/
Copy pathscript.js
File metadata and controls
112 lines (93 loc) · 3.26 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
// Constants
const QUESTION_TIME = 15; // seconds
// Variables
let currentQuestions = [];
let currentQuestionIndex = 0;
let score = 0;
let timer;
// DOM Elements
const loginSection = document.getElementById("login-section");
const quizSection = document.getElementById("quiz-section");
const loginForm = document.getElementById("login-form");
const timerElement = document.getElementById("timer");
const progressBarElement = document.getElementById("progress-bar");
const questionElement = document.getElementById("question");
const optionsElement = document.getElementById("options");
const submitButton = document.getElementById("submit-btn");
// Login Form Handler
loginForm.addEventListener("submit", (e) => {
e.preventDefault();
const name = document.getElementById("name").value;
const age = parseInt(document.getElementById("age").value);
// Set questions based on age
if (age < 17) {
currentQuestions = [...juniorQuestions];
} else {
currentQuestions = [...seniorQuestions];
}
// Hide login, show quiz
loginSection.classList.add("hidden");
quizSection.classList.remove("hidden");
// Start quiz
renderQuestion(currentQuestionIndex);
startTimer();
});
// Render Question
function renderQuestion(index) {
const questionData = currentQuestions[index];
questionElement.textContent = questionData.question;
optionsElement.innerHTML = "";
questionData.options.forEach((option) => {
const li = document.createElement("li");
li.innerHTML = `
<input type="radio" id="${option}" name="answer" value="${option}">
<label for="${option}">${option}</label>
`;
optionsElement.appendChild(li);
});
submitButton.disabled = true;
}
// Handle Answer Selection
optionsElement.addEventListener("change", () => {
submitButton.disabled = false;
});
// Handle Timer
function startTimer() {
let timerValue = QUESTION_TIME;
timerElement.textContent = timerValue;
progressBarElement.style.width = "100%";
timer = setInterval(() => {
timerValue--;
timerElement.textContent = timerValue;
progressBarElement.style.width = `${(timerValue / QUESTION_TIME) * 100}%`;
if (timerValue <= 0) {
clearInterval(timer);
submitAnswer();
}
}, 1000);
}
// Handle Answer Submission
function submitAnswer() {
clearInterval(timer);
const selectedOption = document.querySelector("input[name='answer']:checked");
if (selectedOption && selectedOption.value === currentQuestions[currentQuestionIndex].answer) {
score++;
}
currentQuestionIndex++;
if (currentQuestionIndex < currentQuestions.length) {
renderQuestion(currentQuestionIndex);
startTimer();
} else {
endQuiz();
}
}
// End Quiz
function endQuiz() {
timerElement.style.display = "none";
progressBarElement.style.display = "none";
questionElement.textContent = `Quiz Completed! Your score: ${score}/${currentQuestions.length}`;
optionsElement.innerHTML = "";
submitButton.style.display = "none";
}
// Event Listeners
submitButton.addEventListener("click", submitAnswer);