-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.js
More file actions
74 lines (74 loc) · 1.89 KB
/
Copy pathquiz.js
File metadata and controls
74 lines (74 loc) · 1.89 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
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
let playerbox = document.getElementById("playerbox");
let name1 = localStorage.getItem("name");
let showName = () =>{
playerbox.innerText = "Player : "+name1;
}
showName();
let a1 = [];
let index = 0;
let score = 0;
let loadQuestion = async() =>
{
a1 = await fetch("./quiz.json");
a1 = await a1.json();
console.log(a1);
showQuestion();
}
loadQuestion();
let question = document.getElementById("question");
let options = document.getElementById("options");
let nextBtn = document.getElementById("next-btn");
let showQuestion = () =>
{
current = a1[index];
console.log(current);
question.innerText = current.question;
current.options.forEach(
(str,i) => {
let b1 = document.createElement("button");
b1.innerText = str ;
b1.classList.add("option-btn");
options.appendChild(b1);
b1.addEventListener("click",()=>{
checkAnswer(i)
}
);
console.log(i);
});
}
let nextQuestion = () =>{
index++;
options.innerHTML = "";
if(index>=a1.length)
{
nextBtn.style.display = "none";
question.innerText = "Quiz Completed";
return;
}
loadQuestion();
}
let checkAnswer = (i) =>{
let correctAnswer = current.correct;
if(i===correctAnswer)
{
score++;
scoreTag = document.getElementById("score");
scoreTag.innerText = "Score: "+score;
}
let btnArray = document.querySelectorAll(".option-btn");
btnArray.forEach(
(btn, index)=>
{
btn.disabled = true;
if(index===correctAnswer)
{
btn.style.backgroundColor = "#22c55e";
btn.style.color = "black"
}
else{
btn.style.backgroundColor = "#ef4444";
btn.style.color = "black"
}
}
)
}