-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.js
91 lines (79 loc) · 2.41 KB
/
Script.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
const quiz = [
{
Q: "Q1: Which type of JavaScript language is ?",
a: "Object-Oriented",
b: "Object-Based",
c: "Assembly-language",
d: "High-level",
ans: "ans2",
},
{
Q: "Q2: Which one of the following also known as Conditional Expression:",
a: "Alternative to if-else",
b: "Switch statement",
c: "If-then-else statement",
d: "immediate if",
ans: "ans4",
},
{
Q: "Q3: In JavaScript, what is a block of statement?",
a: "Conditional block",
b: "block that combines a number of statements into a single compound statement",
c: "both conditional block and a single statement",
d: "block that contains a single statement",
ans: "ans2",
},
{
Q: "Q4: When interpreter encounters an empty statements, what it will do:",
a: "Shows a warning",
b: "Prompts to complete the statement",
c: "Throws an error",
d: "Ignores the statements",
ans: "ans4",
},
];
const question = document.querySelector(".question");
const option1 = document.querySelector("#option1");
const option2 = document.querySelector("#option2");
const option3 = document.querySelector("#option3");
const option4 = document.querySelector("#option4");
const submit = document.querySelector("#submit");
const answer = document.querySelectorAll(".answer");
let questionCount = 0;
let score = 0;
const loadquestion = () => {
const questionList = quiz[questionCount];
question.innerText = questionList.Q;
option1.innerText = questionList.a;
option2.innerText = questionList.b;
option3.innerText = questionList.c;
option4.innerText = questionList.d;
};
loadquestion();
const getCheckAnswer = () => {
let Answer;
answer.forEach((curAnsElem) => {
if (curAnsElem.checked) {
Answer = curAnsElem.id;
}
});
return Answer;
};
const deselectAll = () => {
answer.forEach((curAnsElem) => (curAnsElem.checked = false));
};
submit.addEventListener("click", () => {
const checkedAnswer = getCheckAnswer();
console.log(checkedAnswer);
if (checkedAnswer === quiz[questionCount].ans) {
score++;
}
questionCount++;
deselectAll();
if (questionCount < quiz.length) {
loadquestion();
} else {
showscore.innerHTML = ` <h3>You Scored ${score} / ${quiz.length} ✌</h3>
<button class="btn" onclick="location.reload()">Play Again</button>`;
}
});