-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (141 loc) · 5.48 KB
/
index.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
// Import quiz data from separate file
import { multiQuiz } from "./data.js";
const questionContainer = document.getElementById("question-container");
const answerButtons = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-button");
const restartButton = document.getElementById("restart-button");
const resultsContainer = document.getElementById("results-container");
const navBtn = document.getElementById('nav-btn')
const navMenu = document.getElementById('nav-menu')
navBtn.addEventListener('click', function(e) {
navMenu.style.display === 'none' ? (navMenu.style.display = 'flex', e.stopPropagation()) : navMenu.style.display = 'none';
})
document.addEventListener('click', function(e) {
if (!navBtn.contains(e.target) && !navMenu.contains(e.target)) {
navMenu.style.display = 'none'
}
})
const navLinks = navMenu.querySelectorAll('a');
navLinks.forEach(link => {
link.addEventListener('click', function() {
navMenu.style.display = 'none';
});
});
let fontBtn = document.getElementById('font-btn');
let body = document.getElementsByTagName('body')[0];
let altFont = "'Atkinson Hyperlegible', sans-serif";
let defaultFont = "'VT323', monospace";
let font = defaultFont;
fontBtn.addEventListener('click', function() {
font === defaultFont ? (
font = altFont,
fontBtn.textContent = "VT323"
) : (
font = defaultFont,
fontBtn.textContent = "Atkinson",
body.style.fontSize = "inherit"
);
body.style.fontFamily = font;
});
let shuffledQuestions;
let currentQuestionIndex;
let correctAnswers = 0;
let answeredQuestions = 0;
// Event listeners
nextButton.addEventListener("click", showNextQuestion);
restartButton.addEventListener("click", startQuiz);
startQuiz(); // Start quiz when page loads
function startQuiz() {
shuffledQuestions = multiQuiz.sort(() => Math.random() - 0.5);
currentQuestionIndex = 0;
correctAnswers = 0;
answeredQuestions = 0;
resultsContainer.innerText = "";
questionContainer.classList.remove("hide")
nextButton.classList.add("hide"); // Hide "Next" button
answerButtons.style.display = "flex"; // Show answer buttons
showNextQuestion();
}
function showNextQuestion() {
resetState();
showQuestion(shuffledQuestions[currentQuestionIndex]);
currentQuestionIndex++;
restartButton.classList.add("hide")
}
function showQuestion(question) {
questionContainer.innerText = question.question;
question.choices.forEach(choice => {
const button = document.createElement("button");
button.innerText = choice;
button.classList.add("answer-btn");
choice === question.answer ? button.dataset.correct = true : null;
button.addEventListener("click", selectAnswer);
answerButtons.appendChild(button);
});
}
function resetState() {
while (answerButtons.firstChild) answerButtons.removeChild(answerButtons.firstChild);
nextButton.disabled = true;
}
function selectAnswer(e) {
const selectedButton = e.target;
const correct = selectedButton.dataset.correct;
correct ? correctAnswers++ : null;
answeredQuestions++;
setStatusClass(selectedButton, correct, true);
Array.from(answerButtons.children).forEach(button => {
button !== selectedButton ? (
setStatusClass(button, button.dataset.correct, false),
button.disabled = true,
!correct && button.dataset.correct ? button.classList.add("show-correct") : null
) : null;
});
nextButton.disabled = false;
shuffledQuestions.length > currentQuestionIndex ? nextButton.classList.remove("hide") : showResults();
}
function showResults() {
// Remove question and answer buttons
questionContainer.innerText = "";
// answerButtons.style.display = "none";
// Show results and "Take Again?" button
resultsContainer.innerText = `You got ${correctAnswers} out of ${answeredQuestions} correct.`;
nextButton.classList.add("hide");
restartButton.innerText = "Take Quiz Again?";
restartButton.classList.remove("hide");
}
function setStatusClass(element, correct, selected) {
clearStatusClass(element);
selected ? (correct ? element.classList.add("correct") : element.classList.add("incorrect")) : null;
}
function clearStatusClass(element) {
element.classList.remove("correct");
element.classList.remove("incorrect");
}
const skipButton = document.getElementById('skip-button');
const alternativeText = document.getElementById('alternative-text');
const intro = document.getElementById('intro');
const typed = new Typed('#intro', {
strings: [
'function learnJavaScript',
'function learnJavaScript{}',
'function learnJavaScript',
'function learnJavaScript()',
'function learnJavaScript(quiz)...??',
'learnJavaScript%$@!*$!',
"<span class='bold'>function learnJavaScript(quiz) ^1000{</span> <br> ^1000 If you’re anything like me,^1000 you need all the practice you can get to help you remember the syntax and concepts found in JavaScript. ^1000 Through writing both the questions and the code for this quiz, I’m reinforcing everything I’ve learned so far. <br> ^1000I hope you find it useful, and even have some fun!}<br>^1000<a href='#quiz-page' class='start-button link-style'>addEventListener('click', startQuiz)</a>"
],
typeSpeed: 10,
backSpeed: 10,
smartBackspace: true,
loop: false,
onComplete: () => {
skipButton.style.display = 'none';
}
});
// skip button removes typed.js and inserts static text
skipButton.addEventListener('click', () => {
typed.destroy();
intro.style.display = 'none'
skipButton.style.display = 'none';
alternativeText.style.display = 'block';
});