-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (139 loc) · 4.74 KB
/
script.js
File metadata and controls
151 lines (139 loc) · 4.74 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
document.addEventListener('DOMContentLoaded', () => {
startQuiz();
})
const questionContainer = document.getElementById('question-container');
const questionElement = document.getElementById('question');
const answerButtonsElement = document.getElementById('answer-buttons');
const nextButton = document.getElementById('next-btn');
const scoreDisplay = document.getElementById('score');
let currentQuestionIndex = 0;
let score = 0;
function startQuiz() {
showQuestion(questions[currentQuestionIndex]);
}
function showQuestion(question) {
questionElement.innerText = question.question;
clearAnswerButtons();
question.answers.forEach(answer => {
const button = document.createElement('button');
button.innerText = answer.text;
button.classList.add('btn');
button.addEventListener('click', () => selectAnswer(answer, button));
answerButtonsElement.appendChild(button);
});
nextButton.disabled = true;
}
function clearAnswerButtons() {
while (answerButtonsElement.firstChild) {
answerButtonsElement.removeChild(answerButtonsElement.firstChild);
}
}
function selectAnswer(answer, button) {
const buttons = document.querySelectorAll('#answer-buttons .btn');
buttons.forEach(button => {
button.classList.remove('selected');
});
button.classList.add('selected');
if (answer.correct) {
score++;
console.log('Correct!');
} else {
console.log('Wrong answer');
}
nextButton.disabled = false;
}
function nextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(questions[currentQuestionIndex]);
nextButton.disabled = true;
} else {
showScore();
console.log('Quiz completed!');
}
}
function showScore() {
scoreDisplay.innerText = `Your score: ${score}/${questions.length}`;
}
const questions = [
{
question: 'What does HTML stand for?',
answers: [
{ text: 'Hyperlinks and Text Markup Language', correct: false },
{ text: 'Hypertext Markup Language', correct: true },
{ text: 'Home Tool Markup Language', correct: false },
]
},
{
question: 'What is the output of the following code snippet in JavaScript?\nconsole.log(2 + "2");',
answers: [
{ text: '4', correct: false },
{ text: '"22"', correct: true },
{ text: 'Error', correct: false }
]
},
{
question: 'Which sorting algorithm has the worst-case time complexity of O(n^2)?',
answers: [
{ text: 'Merge Sort', correct: false },
{ text: 'Bubble Sort', correct: true },
{ text: 'Insertion Sort', correct: false }
]
},
{
question: 'What is the name of the data structure that follows the Last In, First Out (LIFO) principle?',
answers: [
{ text: 'Heap', correct: false },
{ text: 'Stack', correct: true },
{ text: 'Tree', correct: false }
]
},
{
question: 'Which of the following is NOT a valid programming language?',
answers: [
{ text: 'Python', correct: false },
{ text: 'C++', correct: false },
{ text: 'HTML', correct: true },
]
},
{
question: 'What is the main purpose of CSS?',
answers: [
{ text: 'To define the behavior of a webpage', correct: false },
{ text: 'To define the presentation of a webpage', correct: true },
{ text: 'To define the functionality of a webpage', correct: false }
]
},
{
question: 'In object-oriented programming, what is the term used to describe the ability of an object to take many forms?',
answers: [
{ text: 'Encapsulation', correct: false },
{ text: 'Polymorphism', correct: true },
{ text: 'Inheritance', correct: false },
]
},
{
question: 'Which of the following is NOT a valid HTTP request method?',
answers: [
{ text: 'GET', correct: false },
{ text: 'POST', correct: false },
{ text: 'INSERT', correct: true }
]
},
{
question: 'What does SQL stand for?',
answers: [
{ text: 'Structured Query Language', correct: true },
{ text: 'Sequential Query Language', correct: false },
{ text: 'Structured Question Language', correct: false }
]
},
{
question: 'Which of the following is an example of a non-volatile memory?',
answers: [
{ text: 'RAM', correct: false },
{ text: 'ROM', correct: true },
{ text: 'Cache', correct: false },
]
}
]