forked from shuvadeepmondal/HacKerZGuiDe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquizscript.js
146 lines (122 loc) · 5.46 KB
/
quizscript.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
const questions = [
{
title: "What is the capital of France?",
text: "This city is also known as the City of Light.",
options: ["Berlin", "Madrid", "Paris", "Lisbon"],
correct: 2,
},
{
title: "What is 2 + 2?",
text: "Basic Math skills are key!",
options: ["3", "4", "5", "6"],
correct: 1,
},
{
title: "Which planet is known as the Red Planet?",
text: "It is the fourth planet from the sun.",
options: ["Earth", "Mars", "Jupiter", "Venus"],
correct: 1,
},
];
let score = 0;
let currentQuestionIndex = 0;
let timer;
let timeRemaining = 180;
document.getElementById("startQuiz").addEventListener("click", startQuiz);
document.getElementById("submitQuiz").addEventListener("click", endQuiz);
document.getElementById("restartQuiz").addEventListener("click", restartQuiz);
document.getElementById("submitScoreForm").addEventListener("submit", submitScore);
function startQuiz() {
document.querySelector(".hero").classList.add("hidden");
document.getElementById("quizContainer").classList.remove("hidden");
startTimer();
loadQuestion();
}
function restartQuiz() {
score = 0;
currentQuestionIndex = 0;
timeRemaining = 180;
document.querySelector(".hero").classList.add("hidden");
document.getElementById("quizContainer").classList.remove("hidden");
document.getElementById("resultSection").classList.add("hidden");
document.getElementById("submitQuiz").classList.add("hidden");
startTimer();
loadQuestion();
}
function startTimer() {
timer = setInterval(() => {
timeRemaining--;
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
document.getElementById("timeRemaining").innerText = `${minutes}:${seconds.toString().padStart(2, "0")}`;
if (timeRemaining <= 0) {
clearInterval(timer);
endQuiz();
}
}, 1000);
}
function loadQuestion() {
const question = questions[currentQuestionIndex];
document.getElementById("questionTitle").innerText = question.title;
document.getElementById("questionText").innerText = question.text;
const answerOptions = document.getElementById("answerOptions");
answerOptions.innerHTML = "";
question.options.forEach((option, index) => {
const button = document.createElement("button");
button.innerText = option;
button.classList.add("answer-btn");
button.addEventListener("click", () => selectAnswer(index));
answerOptions.appendChild(button);
});
updateProgressBar();
document.getElementById("nextQuestion").classList.add("hidden"); // Hide next button until an answer is selected
}
function selectAnswer(selectedIndex) {
const correctIndex = questions[currentQuestionIndex].correct;
const buttons = document.querySelectorAll(".answer-btn");
buttons.forEach((button, index) => {
button.disabled = true;
if (index === correctIndex) button.classList.add("correct-answer");
else if (index === selectedIndex) button.classList.add("incorrect-answer");
});
if (selectedIndex === correctIndex) score++;
currentQuestionIndex++;
setTimeout(() => {
if (currentQuestionIndex < questions.length) loadQuestion();
else document.getElementById("submitQuiz").classList.remove("hidden");
}, 2000);
}
function updateProgressBar() {
const progressBar = document.getElementById("progressBar");
const progressPercentage = ((currentQuestionIndex + 1) / questions.length) * 100;
progressBar.style.width = `${progressPercentage}%`;
}
function endQuiz() {
clearInterval(timer);
document.getElementById("quizContainer").classList.add("hidden");
document.getElementById("resultSection").classList.remove("hidden");
document.getElementById("scoreDisplay").innerText = `You scored ${score} out of ${questions.length}`;
const reviewList = document.getElementById("answerReview");
reviewList.innerHTML = "";
questions.forEach((question, index) => {
const li = document.createElement("li");
li.innerHTML = `${question.title} - Correct Answer: ${question.options[question.correct]}`;
reviewList.appendChild(li);
});
setupSocialShare();
}
function setupSocialShare() {
const shareText = `I scored ${score} out of ${questions.length} on the Hacker Quiz! Try it out!`;
document.getElementById("shareFacebook").href = `https://www.facebook.com/sharer/sharer.php?u=https://example.com"e=${encodeURIComponent(shareText)}`;
document.getElementById("shareTwitter").href = `https://twitter.com/intent/tweet?text=${encodeURIComponent(shareText)}&url=https://example.com`;
document.getElementById("shareWhatsApp").href = `https://wa.me/?text=${encodeURIComponent(shareText)}`;
document.getElementById("shareLinkedIn").href = `https://www.linkedin.com/shareArticle?mini=true&url=https://example.com&title=${encodeURIComponent("Hacker Quiz Results")}&summary=${encodeURIComponent(shareText)}`;
document.getElementById("shareInstagram").href = `https://www.instagram.com/`; // No direct Instagram share link
document.getElementById("shareGitHub").href = `https://github.com/`;
document.getElementById("socialShare").classList.remove("hidden");
}
function submitScore(event) {
event.preventDefault();
document.getElementById("submitScoreForm").classList.add("hidden");
document.getElementById("feedbackSection").classList.remove("hidden");
}