1// Quiz Data Repository
2const questions = [
3 {
4 question: "What does Array.prototype.map() return?",
5 options: [
6 "The original array mutated",
7 "A new array of identical length with transformed values",
8 "A single reduced value",
9 "A boolean indicating matching condition"
10 ],
11 answer: 1,
12 explanation: "map() constructs a new array with results from calling the callback on each element."
13 },
14 {
15 question: "Which keyword creates a block-scoped variable in modern JavaScript?",
16 options: ["var", "let", "global", "scope"],
17 answer: 1,
18 explanation: "let and const provide true block-level scoping within curly braces."
19 },
20 {
21 question: "What does Promise.all() do when one of the promises rejects?",
22 options: [
23 "Waits for all to finish",
24 "Immediately rejects with that error (fail-fast)",
25 "Retries the rejected promise",
26 "Returns null"
27 ],
28 answer: 1,
29 explanation: "Promise.all is fail-fast: the first rejection immediately rejects the whole batch."
30 }
31];
32
33// State
34let currentIndex = 0;
35let score = 0;
36
37// Elements
38const questionText = document.querySelector('#question-text');
39const optionsContainer = document.querySelector('#options-container');
40const explanationBox = document.querySelector('#explanation-box');
41const nextBtn = document.querySelector('#next-btn');
42const progressBar = document.querySelector('#progress-bar');
43const questionTracker = document.querySelector('#question-tracker');
44const scoreTracker = document.querySelector('#score-tracker');
45const quizCard = document.querySelector('#quiz-card');
46const resultsCard = document.querySelector('#results-card');
47const finalScoreText = document.querySelector('#final-score-text');
48const scorePercentile = document.querySelector('#score-percentile');
49const restartBtn = document.querySelector('#restart-btn');
50
51function renderQuestion() {
52 const currentQ = questions[currentIndex];
53 questionText.textContent = currentQ.question;
54 explanationBox.classList.add('hidden');
55 nextBtn.classList.add('hidden');
56 optionsContainer.innerHTML = '';
57
58 // Progress Bar update
59 const progressPercent = ((currentIndex) / questions.length) * 100;
60 progressBar.style.width = `${progressPercent}%`;
61 questionTracker.textContent = `Question ${currentIndex + 1} of ${questions.length}`;
62
63 currentQ.options.forEach((optText, index) => {
64 const btn = document.createElement('button');
65 btn.className = 'option-btn';
66 btn.textContent = optText;
67 btn.addEventListener('click', () => handleAnswer(index, btn));
68 optionsContainer.appendChild(btn);
69 });
70}
71
72function handleAnswer(selectedIndex, selectedBtn) {
73 const currentQ = questions[currentIndex];
74 const buttons = optionsContainer.querySelectorAll('.option-btn');
75
76 // Disable all options
77 buttons.forEach(btn => btn.disabled = true);
78
79 // Check correctness
80 if (selectedIndex === currentQ.answer) {
81 selectedBtn.classList.add('correct');
82 score++;
83 scoreTracker.textContent = `Score: ${score}`;
84 } else {
85 selectedBtn.classList.add('wrong');
86 // Highlight correct answer
87 buttons[currentQ.answer].classList.add('correct');
88 }
89
90 // Show explanation
91 explanationBox.textContent = currentQ.explanation;
92 explanationBox.classList.remove('hidden');
93
94 // Reveal next button
95 nextBtn.classList.remove('hidden');
96}
97
98nextBtn.addEventListener('click', () => {
99 currentIndex++;
100 if (currentIndex < questions.length) {
101 renderQuestion();
102 } else {
103 showResults();
104 }
105});
106
107function showResults() {
108 quizCard.classList.add('hidden');
109 nextBtn.classList.add('hidden');
110 resultsCard.classList.remove('hidden');
111 progressBar.style.width = '100%';
112
113 const percent = Math.round((score / questions.length) * 100);
114 finalScoreText.textContent = `You scored ${score} out of ${questions.length}`;
115 scorePercentile.textContent = `${percent}%`;
116}
117
118restartBtn.addEventListener('click', () => {
119 currentIndex = 0;
120 score = 0;
121 scoreTracker.textContent = 'Score: 0';
122 resultsCard.classList.add('hidden');
123 quizCard.classList.remove('hidden');
124 renderQuestion();
125});
126
127// Initialize
128renderQuestion();