-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
40 lines (34 loc) · 1.14 KB
/
script.js
File metadata and controls
40 lines (34 loc) · 1.14 KB
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 form = document.getElementById("test");
const btn = document.getElementById("calculate-score");
const box = document.getElementById("score-display");
const scoreEl = document.getElementById("score");
async function loadQuestions() {
const res = await fetch("questions.txt");
const text = await res.text();
const questions = text
.split("\n")
.map((q) => q.trim())
.filter(Boolean);
questions.forEach((q, i) => {
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = `q${i + 1}`;
label.appendChild(checkbox);
label.append(` ${q}`);
form.appendChild(label);
form.appendChild(document.createElement("br"));
});
}
function calculateScore() {
const total = form.querySelectorAll("input[type=checkbox]").length;
const checked = form.querySelectorAll("input[type=checkbox]:checked").length;
return Math.round(100 - checked * (100 / total));
}
btn.addEventListener("click", () => {
if (box.classList.contains("hidden")) {
box.classList.remove("hidden"); // reveal on first press
}
scoreEl.textContent = calculateScore();
});
loadQuestions();