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>Dynamic Quiz Website</title>
|
| 7 |
+
<style>
|
| 8 |
+
/* Basic styling */
|
| 9 |
+
body {
|
| 10 |
+
font-family: Arial, sans-serif;
|
| 11 |
+
background-color: #f4f4f9;
|
| 12 |
+
color: #333;
|
| 13 |
+
text-align: center;
|
| 14 |
+
padding: 20px;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
h1 {
|
| 18 |
+
font-size: 32px;
|
| 19 |
+
margin-bottom: 20px;
|
| 20 |
+
color: #007BFF;
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
.menu, .quiz, .score, .leaderboard {
|
| 24 |
+
max-width: 600px;
|
| 25 |
+
margin: 0 auto;
|
| 26 |
+
padding: 20px;
|
| 27 |
+
background-color: #fff;
|
| 28 |
+
border-radius: 10px;
|
| 29 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
button {
|
| 33 |
+
padding: 10px 20px;
|
| 34 |
+
background-color: #007BFF;
|
| 35 |
+
color: #fff;
|
| 36 |
+
border: none;
|
| 37 |
+
border-radius: 5px;
|
| 38 |
+
font-size: 16px;
|
| 39 |
+
cursor: pointer;
|
| 40 |
+
transition: background-color 0.3s ease;
|
| 41 |
+
margin: 5px;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
button:hover {
|
| 45 |
+
background-color: #0056b3;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.question {
|
| 49 |
+
margin-bottom: 20px;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
.options {
|
| 53 |
+
display: flex;
|
| 54 |
+
flex-direction: column;
|
| 55 |
+
align-items: center;
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
.options button {
|
| 59 |
+
width: 80%;
|
| 60 |
+
margin: 5px 0;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
.score {
|
| 64 |
+
font-size: 24px;
|
| 65 |
+
margin-top: 20px;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.progress-bar {
|
| 69 |
+
width: 100%;
|
| 70 |
+
height: 10px;
|
| 71 |
+
background-color: #e0e0e0;
|
| 72 |
+
border-radius: 5px;
|
| 73 |
+
margin-bottom: 20px;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.progress {
|
| 77 |
+
height: 100%;
|
| 78 |
+
background-color: #007BFF;
|
| 79 |
+
border-radius: 5px;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.timer {
|
| 83 |
+
font-size: 20px;
|
| 84 |
+
margin-bottom: 20px;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
.hint {
|
| 88 |
+
color: #ff5722;
|
| 89 |
+
cursor: pointer;
|
| 90 |
+
margin-top: 10px;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.leaderboard {
|
| 94 |
+
margin-top: 20px;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
.leaderboard table {
|
| 98 |
+
width: 100%;
|
| 99 |
+
border-collapse: collapse;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.leaderboard th, .leaderboard td {
|
| 103 |
+
padding: 10px;
|
| 104 |
+
border: 1px solid #ddd;
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
.leaderboard th {
|
| 108 |
+
background-color: #007BFF;
|
| 109 |
+
color: #fff;
|
| 110 |
+
}
|
| 111 |
+
</style>
|
| 112 |
+
</head>
|
| 113 |
+
<body>
|
| 114 |
+
|
| 115 |
+
<!-- Main Menu -->
|
| 116 |
+
<div id="menu" class="menu">
|
| 117 |
+
<h1>Dynamic Quiz Website</h1>
|
| 118 |
+
<p>Choose a topic and difficulty to start the quiz:</p>
|
| 119 |
+
<select id="difficulty">
|
| 120 |
+
<option value="easy">Easy</option>
|
| 121 |
+
<option value="medium">Medium</option>
|
| 122 |
+
<option value="hard">Hard</option>
|
| 123 |
+
</select>
|
| 124 |
+
<button onclick="startQuiz('math')">Math</button>
|
| 125 |
+
<button onclick="startQuiz('chemistry')">Chemistry</button>
|
| 126 |
+
<button onclick="startQuiz('biology')">Biology</button>
|
| 127 |
+
<button onclick="startQuiz('science')">Science</button>
|
| 128 |
+
<button onclick="startQuiz('history')">History/Social Studies</button>
|
| 129 |
+
<button onclick="startQuiz('language')">Language</button>
|
| 130 |
+
<button onclick="showLeaderboard()">Leaderboard</button>
|
| 131 |
+
</div>
|
| 132 |
+
|
| 133 |
+
<!-- Quiz Section -->
|
| 134 |
+
<div id="quiz" class="quiz" style="display: none;">
|
| 135 |
+
<h1 id="quizTopic"></h1>
|
| 136 |
+
<div class="progress-bar">
|
| 137 |
+
<div class="progress" id="progress"></div>
|
| 138 |
+
</div>
|
| 139 |
+
<div class="timer" id="timer">Time left: 10</div>
|
| 140 |
+
<div class="question">
|
| 141 |
+
<p id="questionText"></p>
|
| 142 |
+
</div>
|
| 143 |
+
<div class="options" id="options"></div>
|
| 144 |
+
<div class="hint" onclick="useHint()">Get a hint (-2 points)</div>
|
| 145 |
+
<button onclick="backToMenu()">Back to Menu</button>
|
| 146 |
+
</div>
|
| 147 |
+
|
| 148 |
+
<!-- Score Section -->
|
| 149 |
+
<div id="score" class="score" style="display: none;">
|
| 150 |
+
<h1>Your Score</h1>
|
| 151 |
+
<p id="scoreText"></p>
|
| 152 |
+
<button onclick="backToMenu()">Back to Menu</button>
|
| 153 |
+
<button onclick="shareResult()">Share Result</button>
|
| 154 |
+
</div>
|
| 155 |
+
|
| 156 |
+
<!-- Leaderboard Section -->
|
| 157 |
+
<div id="leaderboard" class="leaderboard" style="display: none;">
|
| 158 |
+
<h1>Leaderboard</h1>
|
| 159 |
+
<table id="leaderboardTable">
|
| 160 |
+
<thead>
|
| 161 |
+
<tr>
|
| 162 |
+
<th>Rank</th>
|
| 163 |
+
<th>Username</th>
|
| 164 |
+
<th>Score</th>
|
| 165 |
+
</tr>
|
| 166 |
+
</thead>
|
| 167 |
+
<tbody>
|
| 168 |
+
<!-- Leaderboard data will be populated here -->
|
| 169 |
+
</tbody>
|
| 170 |
+
</table>
|
| 171 |
+
<button onclick="backToMenu()">Back to Menu</button>
|
| 172 |
+
</div>
|
| 173 |
+
|
| 174 |
+
<script>
|
| 175 |
+
let currentTopic = "";
|
| 176 |
+
let difficulty = "easy";
|
| 177 |
+
let score = 0;
|
| 178 |
+
let questionCount = 0;
|
| 179 |
+
let timer;
|
| 180 |
+
let timeLeft = 10;
|
| 181 |
+
let hintUsed = false;
|
| 182 |
+
let currentHint = "";
|
| 183 |
+
let correctAnswers = 0; // Track correct answers
|
| 184 |
+
|
| 185 |
+
// Leaderboard data (mock data for now)
|
| 186 |
+
const leaderboardData = [
|
| 187 |
+
{ username: "Alice", score: 95 },
|
| 188 |
+
{ username: "Bob", score: 90 },
|
| 189 |
+
{ username: "Charlie", score: 85 },
|
| 190 |
+
];
|
| 191 |
+
|
| 192 |
+
// Function to generate random math questions based on difficulty
|
| 193 |
+
function generateMathQuestion() {
|
| 194 |
+
const num1 = Math.floor(Math.random() * (difficulty === "easy" ? 10 : difficulty === "medium" ? 20 : 50)) + 1;
|
| 195 |
+
const num2 = Math.floor(Math.random() * (difficulty === "easy" ? 10 : difficulty === "medium" ? 20 : 50)) + 1;
|
| 196 |
+
const operators = ["+", "-", "*"];
|
| 197 |
+
const operator = operators[Math.floor(Math.random() * operators.length)];
|
| 198 |
+
const question = `What is ${num1} ${operator} ${num2}?`;
|
| 199 |
+
const answer = eval(`${num1} ${operator} ${num2}`).toString();
|
| 200 |
+
const options = [answer];
|
| 201 |
+
while (options.length < 4) {
|
| 202 |
+
const randomOption = Math.floor(Math.random() * (difficulty === "easy" ? 20 : difficulty === "medium" ? 50 : 100)) + 1;
|
| 203 |
+
if (!options.includes(randomOption.toString())) {
|
| 204 |
+
options.push(randomOption.toString());
|
| 205 |
+
}
|
| 206 |
+
}
|
| 207 |
+
options.sort(() => Math.random() - 0.5); // Shuffle options
|
| 208 |
+
|
| 209 |
+
// Hint for math question
|
| 210 |
+
currentHint = "Think about the order of operations (PEMDAS/BODMAS).";
|
| 211 |
+
return { question, options, answer };
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
// Function to generate random chemistry questions
|
| 215 |
+
function generateChemistryQuestion() {
|
| 216 |
+
const questions = [
|
| 217 |
+
{
|
| 218 |
+
question: "What is the chemical formula for water?",
|
| 219 |
+
answer: "H2O",
|
| 220 |
+
options: ["H2O", "CO2", "O2", "NaCl"],
|
| 221 |
+
hint: "This compound is essential for life and covers most of the Earth's surface."
|
| 222 |
+
},
|
| 223 |
+
{
|
| 224 |
+
question: "What is the common name for H2O?",
|
| 225 |
+
answer: "Water",
|
| 226 |
+
options: ["Water", "Carbon Dioxide", "Oxygen", "Sodium Chloride"],
|
| 227 |
+
hint: "This compound is commonly found in lakes, rivers, and oceans."
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
question: "Which compound is a greenhouse gas?",
|
| 231 |
+
answer: "CO2",
|
| 232 |
+
options: ["H2O", "CO2", "O2", "NaCl"],
|
| 233 |
+
hint: "This compound is released when fossil fuels are burned."
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
question: "Balance the equation: H2 + O2 → H2O.",
|
| 237 |
+
answer: "2H2 + O2 → 2H2O",
|
| 238 |
+
options: ["H2 + O2 → H2O", "2H2 + O2 → 2H2O", "H2 + 2O2 → 2H2O", "2H2 + 2O2 → 2H2O"],
|
| 239 |
+
hint: "Remember that the number of atoms on both sides of the equation must be equal."
|
| 240 |
+
}
|
| 241 |
+
];
|
| 242 |
+
|
| 243 |
+
const randomQuestion = questions[Math.floor(Math.random() * questions.length)];
|
| 244 |
+
currentHint = randomQuestion.hint;
|
| 245 |
+
return randomQuestion;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
// Function to generate random biology questions
|
| 249 |
+
function generateBiologyQuestion() {
|
| 250 |
+
const questions = [
|
| 251 |
+
{
|
| 252 |
+
question: "Which organelle is known as the powerhouse of the cell?",
|
| 253 |
+
answer: "Mitochondria",
|
| 254 |
+
options: ["Nucleus", "Mitochondria", "Ribosome", "Chloroplast"],
|
| 255 |
+
hint: "This organelle produces energy in the form of ATP."
|
| 256 |
+
},
|
| 257 |
+
{
|
| 258 |
+
question: "What is the function of the ribosome?",
|
| 259 |
+
answer: "Protein synthesis",
|
| 260 |
+
options: ["Energy production", "Protein synthesis", "DNA replication", "Waste removal"],
|
| 261 |
+
hint: "This organelle is responsible for creating proteins."
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
question: "Which part of the human body is responsible for pumping blood?",
|
| 265 |
+
answer: "Heart",
|
| 266 |
+
options: ["Brain", "Heart", "Liver", "Lungs"],
|
| 267 |
+
hint: "This organ is part of the circulatory system."
|
| 268 |
+
},
|
| 269 |
+
{
|
| 270 |
+
question: "What is the largest organ in the human body?",
|
| 271 |
+
answer: "Skin",
|
| 272 |
+
options: ["Liver", "Skin", "Brain", "Heart"],
|
| 273 |
+
hint: "This organ protects the body from external damage."
|
| 274 |
+
}
|
| 275 |
+
];
|
| 276 |
+
|
| 277 |
+
const randomQuestion = questions[Math.floor(Math.random() * questions.length)];
|
| 278 |
+
currentHint = randomQuestion.hint;
|
| 279 |
+
return randomQuestion;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
// Function to generate random history/social studies questions
|
| 283 |
+
function generateHistoryQuestion() {
|
| 284 |
+
const questions = [
|
| 285 |
+
{
|
| 286 |
+
question: "Who was the first President of the United States?",
|
| 287 |
+
answer: "George Washington",
|
| 288 |
+
options: ["Thomas Jefferson", "George Washington", "Abraham Lincoln", "John Adams"],
|
| 289 |
+
hint: "He is often referred to as the 'Father of His Country'."
|
| 290 |
+
},
|
| 291 |
+
{
|
| 292 |
+
question: "In which year did World War II end?",
|
| 293 |
+
answer: "1945",
|
| 294 |
+
options: ["1939", "1945", "1950", "1960"],
|
| 295 |
+
hint: "The war ended after the surrender of Japan."
|
| 296 |
+
},
|
| 297 |
+
{
|
| 298 |
+
question: "Which document declared the independence of the United States?",
|
| 299 |
+
answer: "Declaration of Independence",
|
| 300 |
+
options: ["Constitution", "Declaration of Independence", "Bill of Rights", "Magna Carta"],
|
| 301 |
+
hint: "This document was adopted on July 4, 1776."
|
| 302 |
+
},
|
| 303 |
+
{
|
| 304 |
+
question: "Who wrote 'The Communist Manifesto'?",
|
| 305 |
+
answer: "Karl Marx and Friedrich Engels",
|
| 306 |
+
options: ["Karl Marx and Friedrich Engels", "Vladimir Lenin", "Joseph Stalin", "Mao Zedong"],
|
| 307 |
+
hint: "This book was published in 1848."
|
| 308 |
+
}
|
| 309 |
+
];
|
| 310 |
+
|
| 311 |
+
const randomQuestion = questions[Math.floor(Math.random() * questions.length)];
|
| 312 |
+
currentHint = randomQuestion.hint;
|
| 313 |
+
return randomQuestion;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
// Function to generate random language questions
|
| 317 |
+
function generateLanguageQuestion() {
|
| 318 |
+
const questions = [
|
| 319 |
+
{
|
| 320 |
+
question: "What is the past tense of 'go'?",
|
| 321 |
+
answer: "Went",
|
| 322 |
+
options: ["Goed", "Went", "Gone", "Going"],
|
| 323 |
+
hint: "This verb is irregular and does not follow the standard past tense rules."
|
| 324 |
+
},
|
| 325 |
+
{
|
| 326 |
+
question: "What is the plural of 'child'?",
|
| 327 |
+
answer: "Children",
|
| 328 |
+
options: ["Childs", "Children", "Childen", "Childe"],
|
| 329 |
+
hint: "This word is an irregular plural."
|
| 330 |
+
},
|
| 331 |
+
{
|
| 332 |
+
question: "Which word is a synonym for 'happy'?",
|
| 333 |
+
answer: "Joyful",
|
| 334 |
+
options: ["Sad", "Joyful", "Angry", "Bored"],
|
| 335 |
+
hint: "This word means feeling or showing pleasure."
|
| 336 |
+
},
|
| 337 |
+
{
|
| 338 |
+
question: "What is the main verb in the sentence: 'She is reading a book.'?",
|
| 339 |
+
answer: "Reading",
|
| 340 |
+
options: ["She", "Is", "Reading", "Book"],
|
| 341 |
+
hint: "This word describes the action in the sentence."
|
| 342 |
+
}
|
| 343 |
+
];
|
| 344 |
+
|
| 345 |
+
const randomQuestion = questions[Math.floor(Math.random() * questions.length)];
|
| 346 |
+
currentHint = randomQuestion.hint;
|
| 347 |
+
return randomQuestion;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
// Function to start the quiz
|
| 351 |
+
function startQuiz(topic) {
|
| 352 |
+
currentTopic = topic;
|
| 353 |
+
difficulty = document.getElementById("difficulty").value;
|
| 354 |
+
score = 0;
|
| 355 |
+
questionCount = 0;
|
| 356 |
+
correctAnswers = 0; // Reset correct answers counter
|
| 357 |
+
document.getElementById("menu").style.display = "none";
|
| 358 |
+
document.getElementById("quiz").style.display = "block";
|
| 359 |
+
document.getElementById("quizTopic").innerText = topic.charAt(0).toUpperCase() + topic.slice(1);
|
| 360 |
+
timeLeft = 10; // Reset timeLeft
|
| 361 |
+
document.getElementById("timer").innerText = `Time left: ${timeLeft}`;
|
| 362 |
+
loadQuestion();
|
| 363 |
+
startTimer();
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
// Function to load the next question
|
| 367 |
+
function loadQuestion() {
|
| 368 |
+
let questionData;
|
| 369 |
+
switch (currentTopic) {
|
| 370 |
+
case "math":
|
| 371 |
+
questionData = generateMathQuestion();
|
| 372 |
+
break;
|
| 373 |
+
case "chemistry":
|
| 374 |
+
questionData = generateChemistryQuestion();
|
| 375 |
+
break;
|
| 376 |
+
case "biology":
|
| 377 |
+
questionData = generateBiologyQuestion();
|
| 378 |
+
break;
|
| 379 |
+
case "history":
|
| 380 |
+
questionData = generateHistoryQuestion();
|
| 381 |
+
break;
|
| 382 |
+
case "language":
|
| 383 |
+
questionData = generateLanguageQuestion();
|
| 384 |
+
break;
|
| 385 |
+
default:
|
| 386 |
+
questionData = { question: "No question available", options: [], answer: "" };
|
| 387 |
+
}
|
| 388 |
+
document.getElementById("questionText").innerText = questionData.question;
|
| 389 |
+
const optionsDiv = document.getElementById("options");
|
| 390 |
+
optionsDiv.innerHTML = "";
|
| 391 |
+
questionData.options.forEach(option => {
|
| 392 |
+
const button = document.createElement("button");
|
| 393 |
+
button.innerText = option;
|
| 394 |
+
button.onclick = () => checkAnswer(option, questionData.answer);
|
| 395 |
+
optionsDiv.appendChild(button);
|
| 396 |
+
});
|
| 397 |
+
updateProgressBar();
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// Function to check the answer
|
| 401 |
+
function checkAnswer(selected, correct) {
|
| 402 |
+
if (selected === correct) {
|
| 403 |
+
correctAnswers++; // Increment correct answers counter
|
| 404 |
+
score += hintUsed ? 1 : 2; // Deduct points if hint was used
|
| 405 |
+
}
|
| 406 |
+
hintUsed = false;
|
| 407 |
+
questionCount++;
|
| 408 |
+
if (questionCount % 10 === 0) {
|
| 409 |
+
showScore();
|
| 410 |
+
} else {
|
| 411 |
+
loadQuestion();
|
| 412 |
+
}
|
| 413 |
+
}
|
| 414 |
+
|
| 415 |
+
// Function to show the score
|
| 416 |
+
function showScore() {
|
| 417 |
+
clearInterval(timer);
|
| 418 |
+
document.getElementById("quiz").style.display = "none";
|
| 419 |
+
document.getElementById("score").style.display = "block";
|
| 420 |
+
document.getElementById("scoreText").innerText = `You scored ${correctAnswers * 5} points!`;
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
// Function to go back to the main menu
|
| 424 |
+
function backToMenu() {
|
| 425 |
+
clearInterval(timer);
|
| 426 |
+
document.getElementById("quiz").style.display = "none";
|
| 427 |
+
document.getElementById("score").style.display = "none";
|
| 428 |
+
document.getElementById("leaderboard").style.display = "none";
|
| 429 |
+
document.getElementById("menu").style.display = "block";
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
// Function to start the timer
|
| 433 |
+
function startTimer() {
|
| 434 |
+
timer = setInterval(() => {
|
| 435 |
+
timeLeft--;
|
| 436 |
+
document.getElementById("timer").innerText = `Time left: ${timeLeft}`;
|
| 437 |
+
if (timeLeft <= 0 || questionCount >= 10) { // Check if time is up OR 10 questions have been answered
|
| 438 |
+
clearInterval(timer);
|
| 439 |
+
showScore();
|
| 440 |
+
}
|
| 441 |
+
}, 1000);
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
// Function to update the progress bar
|
| 445 |
+
function updateProgressBar() {
|
| 446 |
+
const progress = (questionCount / 10) * 100;
|
| 447 |
+
document.getElementById("progress").style.width = `${progress}%`;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
// Function to use a hint
|
| 451 |
+
function useHint() {
|
| 452 |
+
hintUsed = true;
|
| 453 |
+
alert(`Hint: ${currentHint}`);
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
// Function to show the leaderboard
|
| 457 |
+
function showLeaderboard() {
|
| 458 |
+
document.getElementById("menu").style.display = "none";
|
| 459 |
+
document.getElementById("leaderboard").style.display = "block";
|
| 460 |
+
const leaderboardTable = document.getElementById("leaderboardTable").getElementsByTagName("tbody")[0];
|
| 461 |
+
leaderboardTable.innerHTML = "";
|
| 462 |
+
leaderboardData.sort((a, b) => b.score - a.score).forEach((entry, index) => {
|
| 463 |
+
const row = leaderboardTable.insertRow();
|
| 464 |
+
row.insertCell().innerText = index + 1;
|
| 465 |
+
row.insertCell().innerText = entry.username;
|
| 466 |
+
row.insertCell().innerText = entry.score;
|
| 467 |
+
});
|
| 468 |
+
}
|
| 469 |
+
|
| 470 |
+
// Function to share the result on social media
|
| 471 |
+
function shareResult() {
|
| 472 |
+
const shareText = `I scored ${score} points on the Dynamic Quiz Website! Can you beat me?`;
|
| 473 |
+
const shareUrl = window.location.href;
|
| 474 |
+
window.open(`https://twitter.com/intent/tweet?text=${encodeURIComponent(shareText)}&url=${encodeURIComponent(shareUrl)}`, '_blank');
|
| 475 |
+
}
|
| 476 |
+
</script>
|
| 477 |
+
|
| 478 |
+
</body>
|
| 479 |
+
</html>
|