-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (34 loc) · 911 Bytes
/
app.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
const correctAnswers = ["B", "B", "A", "B"];
const form = document.querySelector(".quiz-form");
const resultContainer = document.querySelector(".result");
let finalScore = document.querySelector(".result span");
form.addEventListener("submit", e => {
e.preventDefault();
let score = 0;
const userAnswers = [
form.q1.value,
form.q2.value,
form.q3.value,
form.q4.value
];
// check answers
userAnswers.forEach((answer, index) => {
if (answer === correctAnswers[index]) {
score += 25;
}
});
// console.log(score);
// show score on page
scrollTo(0, 0);
resultContainer.classList.remove("d-none");
// animate score
let points = 0;
const timer = setInterval(() => {
finalScore.textContent = `${points}%`;
if (points === score) {
clearInterval(timer);
} else {
points++;
}
}, 10);
});