-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (127 loc) · 5.37 KB
/
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
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
let data = [
{
ques: "1. Which type of JavaScript language is",
option1: "a. Object-Oriented",
option2: "b. Object-Based",
option3: "c. Assembly-language",
option4: "d. High-level",
answer: "a. Object-Based",
},
{
ques: "2. Which one of the following also known as Conditional Expression:",
option1: "a. Alternative to if-else",
option2: "b. Switch statement",
option3: "c. If-then-else statement",
option4: "d. immediate if",
answer: "d. immediate if",
},
{
ques: "3. In JavaScript, what is a block of statement?",
option1: "a. Conditional block",
option2:
"b. block that combines a number of statements into a single compound statement",
option3: "c. both conditional block and a single statement",
option4: "d. block that contains a single statement",
answer:
"b. block that combines a number of statements into a single compound statement",
},
{
ques: "4. The 'function' and 'var' are known as:",
option1: "a. Keywords",
option2: "b. Data types",
option3: "c. Declaration statements",
option4: "d. Prototypes",
answer: "c. Declaration statements",
},
{
ques: "5. Which of the following type of a variable is volatile?",
option1: "a. Mutable variable",
option2: "b. Dynamic variable",
option3: "c. Volatile variable",
option4: "d. Immutable variable",
answer: "a. Mutable variable",
},
];
let answers = {};
let index = 0;
window.onload = () => {
document.getElementById("ques").innerText = data[index].ques;
document.getElementById("option1Text").innerText = data[index].option1;
document.getElementById("option2Text").innerText = data[index].option2;
document.getElementById("option3Text").innerText = data[index].option3;
document.getElementById("option4Text").innerText = data[index].option4;
document.getElementById("option1").setAttribute("value", data[index].option1);
document.getElementById("option2").setAttribute("value", data[index].option2);
document.getElementById("option3").setAttribute("value", data[index].option3);
document.getElementById("option4").setAttribute("value", data[index].option4);
for (let i = 1; i < 5; i++) {
document.getElementById("option" + i).setAttribute("name", "ques" + index);
}
document.getElementById("prevBtn").setAttribute("disabled", "true");
};
document.getElementById("prevBtn").addEventListener("click", () => {
index -= 1;
if (index == 0)
document.getElementById("prevBtn").setAttribute("disabled", "true");
for (let i = 1; i < 5; i++) {
document.getElementById("option" + i).setAttribute("name", "ques" + index);
}
document.getElementById("ques").innerText = data[index].ques;
document.getElementById("option1Text").innerText = data[index].option1;
document.getElementById("option2Text").innerText = data[index].option2;
document.getElementById("option3Text").innerText = data[index].option3;
document.getElementById("option4Text").innerText = data[index].option4;
document.getElementById("option1").setAttribute("value", data[index].option1);
document.getElementById("option2").setAttribute("value", data[index].option2);
document.getElementById("option3").setAttribute("value", data[index].option3);
document.getElementById("option4").setAttribute("value", data[index].option4);
});
document.getElementById("nextBtn").addEventListener("click", () => {
answers[index] = document.querySelector(
'input[name="ques' + index + '"]:checked'
).value;
if (document.getElementById("nextBtn").innerText == "Submit") {
let correctAnswers = 0;
for (let i = 0; i < 5; i++) {
if (answers[i] == data[i].answer) {
correctAnswers++;
}
}
document.getElementById("quesPanel").classList.add("d-none");
document.getElementById("resultPanel").classList.remove("d-none");
document.getElementById("totalScore").innerText = correctAnswers+1 + "/5";
}
index += 1;
if (index != 0)
document.getElementById("prevBtn").removeAttribute("disabled");
for (let i = 1; i < 5; i++) {
document.getElementById("option" + i).setAttribute("name", "ques" + index);
}
var ele = document.getElementsByName("ques" + index);
for(var i=0;i<ele.length;i++)
ele[i].checked = false;
if (index == 4) {
document.getElementById("nextBtn").innerText = "Submit";
document.getElementById("nextBtn").classList.remove("btn-primary");
document.getElementById("nextBtn").classList.add("btn-success");
}
if (index < 5) {
document.getElementById("ques").innerText = data[index].ques;
document.getElementById("option1Text").innerText = data[index].option1;
document.getElementById("option2Text").innerText = data[index].option2;
document.getElementById("option3Text").innerText = data[index].option3;
document.getElementById("option4Text").innerText = data[index].option4;
document
.getElementById("option1")
.setAttribute("value", data[index].option1);
document
.getElementById("option2")
.setAttribute("value", data[index].option2);
document
.getElementById("option3")
.setAttribute("value", data[index].option3);
document
.getElementById("option4")
.setAttribute("value", data[index].option4);
}
});