-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.js
More file actions
221 lines (210 loc) · 7.02 KB
/
quiz.js
File metadata and controls
221 lines (210 loc) · 7.02 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const questions = [
{
question: "Who is the father of C language?",
answers: [
{ text: "Steve Jobs", correct: false},
{ text: "James Gosling", correct: false},
{text: "Dennis Ritchie", correct: true},
{text: "Rasmus Lerdorf", correct: false},
]
},
{
question: "Which of the following is not a valid C variable name?",
answers: [
{ text: "int number;", correct: false},
{ text: "float rate;", correct: false},
{text: "int variable_count;", correct: false},
{text: "int$main;", correct: true},
]
},
{
question: "All keywords in C are in ____________",
answers: [
{ text: "LowerCase letters", correct: true},
{ text: "UpperCase letters", correct: false},
{text: "CamelCase letters", correct: false},
{text: "None of the above", correct: false},
]
},
{
question: "Which of the following is true for variable names in C?",
answers: [
{ text: "They can contain alphanumeric characters as well as special characters", correct: false},
{ text: "It is not an error to declare a variable to be one of the keywords(like goto, static)", correct: false},
{text: "Variable names cannot start with a digit", correct: true},
{text: "Variable can be of any length", correct: false},
]
},
{
question: "Which is valid C expression?",
answers: [
{ text: "int my_num = 100,000;", correct: false},
{ text: "int my_num = 100000;", correct: true},
{text: "int my num = 1000;", correct: false},
{text: "int $my_num = 10000;", correct: false},
]
},
{
question: "Which of the following cannot be a variable name in C?",
answers: [
{ text: "volatile", correct: true},
{ text: "true", correct: false},
{text: "friend", correct: false},
{text: "export", correct: false},
]
},
{
question: "What is short int in C programming?",
answers: [
{ text: "The basic data type of C", correct: false },
{ text: "Qualifier", correct: false },
{ text: "Short is the qualifier and int is the basic data type", correct: true },
{ text: "All of the mentioned", correct: false }
]
},
{
question: "Which of the following declaration is not supported by C language?",
answers: [
{ text: "String str;", correct: true },
{ text: "char *str;", correct: false },
{ text: "float str = 3e2;", correct: false },
{ text: "Both “String str;” and “float str = 3e2;”", correct: false },
]
},
{
question: "Which keyword is used to prevent any changes in the variable within a C program?",
answers: [
{ text: "immutable", correct: false },
{ text: "mutable", correct: false },
{ text: "const", correct: true },
{ text: "volatile", correct: false },
]
},
{
question: "What is the result of logical or relational expression in C?",
answers: [
{ text: "True or False", correct: false },
{ text: "0 or 1", correct: true },
{ text: "0 if an expression is false and any positive number if an expression is true", correct: false },
{ text: "None of the mentioned", correct: false },
]
},
{
question: "Which of the following typecasting is accepted by C language?",
answers: [
{ text: "Widening conversions", correct: false },
{ text: "Narrowing conversions", correct: false },
{ text: "Widening & Narrowing conversions", correct:true },
{ text: "None of the mentioned", correct:false },
]
},
{
question: "Where in C the order of precedence of operators do not exist?",
answers: [
{ text: "Within conditional statements, if, else", correct: false },
{ text: "Within while, do-while", correct: false },
{ text: "Within a macro definition", correct: false },
{ text: "None of the mentioned", correct: true },
]
},
{
question: "Which of the following is NOT possible with any 2 operators in C?",
answers: [
{ text: "Different precedence, same associativity", correct: false },
{ text: "Different precedence, different associativity", correct: false },
{ text: "Same precedence, different associativity", correct: true },
{ text: "All of the mentioned", correct: false}
]
},
{
question: "What is an example of iteration in C?",
answers: [
{ text: "for", correct: false },
{ text: "while", correct: false },
{ text: "do-while", correct: false },
{ text: "all of the mentioned", correct: true },
]
},
{
question: "Functions can return enumeration constants in C?",
answers: [
{ text: "true", correct: true },
{ text: "false", correct: false },
{ text: "depends on the compiler", correct: false },
{ text: "depends on the standard", correct: false },
]
},
]
const questionElement = document.getElementById("question");
const answerButtons = document. getElementById("answer-buttons");
const nextButton = document. getElementById("next-btn");
let currentQuestionIndex = 0;
let score = 0;
function startQuiz(){
currentQuestionIndex = 0;
score = 0;
nextButton. innerHTML = "Next";
showQuestion();
}
function showQuestion(){
resetState();
let currentQuestion = questions[currentQuestionIndex];
let questionNo = currentQuestionIndex + 1;
questionElement. innerHTML = questionNo + ". " + currentQuestion.question;
currentQuestion.answers. forEach(answer => {
const button = document. createElement ("button");
button. innerHTML = answer.text;
button.classList.add ("btn");
answerButtons.appendChild(button);
if(answer.correct){
button.dataset.correct = answer.correct;
}
button.addEventListener("click", selectAnswer);
});
}
function resetState(){
nextButton.style.display = "none";
while(answerButtons.firstChild){
answerButtons.removeChild(answerButtons.firstChild)
}
}
function selectAnswer(e){
const selectedbtn = e.target;
const isCorrect = selectedbtn.dataset. correct === "true";
if(isCorrect){
selectedbtn.classList.add("correct");
score++;
}
else{
selectedbtn.classList.add("incorrect");
}
Array.from(answerButtons.children).forEach(button => {
if(button.dataset.correct === "true"){
button.classList.add("correct");
}
button.disabled = true;
});
nextButton.style.display = "block"
}
function showScore() {
resetState();
questionElement.innerHTML = `You scored ${score} out of ${questions.length}!`;
nextButton.innerHTML = "Play Again";
nextButton.style.display = "block";
}
function handleNextButton(){
currentQuestionIndex++;
if(currentQuestionIndex < questions.length){
showQuestion();
}else{
showScore();
}
}
nextButton.addEventListener("click", ()=>{
if(currentQuestionIndex < questions.length){
handleNextButton();
}else{
startQuiz();
}
})
startQuiz();