-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (74 loc) · 2.58 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
let questions = [
{
question: 'Which HTML tag is used to define an inline style?',
choice1: '<script>',
choice2: '<css>',
choice3: '<style>',
choice4: '<span>',
answer: 3,
},
{
question: 'Which property is used to change the text color in CSS?',
choice1: 'text-color',
choice2: 'font-color',
choice3: 'text-style',
choice4: 'color',
answer: 4,
},
{
question: 'Which of the following is the correct way to comment in HTML?',
choice1: '// Comment',
choice2: '<!-- Comment -->',
choice3: '/* Comment */',
choice4: '<! Comment>',
answer: 2,
},
];
let score = 0;
let currentQuestionIndex = 0;
const questionContainer = document.getElementById('question-container');
const choicesContainer = document.getElementById('choices-container');
const questionElement = questionContainer.querySelector('.question');
const choiceButtons = choicesContainer.querySelectorAll('.choice');
const questionNumberElement = document.getElementById('question-number');
const scoreElement = document.getElementById('score');
function startQuiz() {
showQuestion(questions[currentQuestionIndex]);
}
function showQuestion(question) {
questionElement.innerText = question.question;
const choices = [question.choice1, question.choice2, question.choice3, question.choice4];
for (let i = 0; i < choices.length; i++) {
choiceButtons[i].innerText = choices[i];
choiceButtons[i].className = 'choice';
choiceButtons[i].onclick = () => selectAnswer(i + 1,choiceButtons[i]);
}
}
function selectAnswer(selectedIndex, buttonElement) {
const correctIndex = questions[currentQuestionIndex].answer;
if (selectedIndex === correctIndex) {
buttonElement.classList.add('correct');
score++;
} else {
buttonElement.classList.add('incorrect');
choiceButtons[correctIndex - 1].classList.add('correct');
}
updateScore();
setTimeout(() => {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion(questions[currentQuestionIndex]);
} else {
endQuiz();
}
}, 500);
}
function updateScore() {
if(currentQuestionIndex!==2)questionNumberElement.innerText = `Question: ${currentQuestionIndex + 2}`;
scoreElement.innerText = `Score: ${score*10}/30`;
}
function endQuiz() {
localStorage.setItem('quizScore', String(score*10));
window.location.href = "endPage.html";
}
document.addEventListener('DOMContentLoaded', startQuiz);