-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (70 loc) · 3.06 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
let questions = [];
function addQuestion() {
const questionInput = document.getElementById('question');
const answerInputs = document.getElementsByName('answer');
const correctAnswerInput = document.querySelector('input[name="correct-answer"]:checked');
if (questionInput.value === '' || correctAnswerInput === null) {
alert('Please fill out the question and select the correct answer.');
return;
}
const answers = Array.from(answerInputs).map(input => input.value);
const correctAnswerIndex = correctAnswerInput.value;
questions.push({
question: questionInput.value,
answers: answers,
correctAnswer: correctAnswerIndex
});
questionInput.value = '';
answerInputs.forEach(input => input.value = '');
correctAnswerInput.checked = false;
alert('Question added!');
}
function createQuiz() {
if (questions.length === 0) {
alert('Please add at least one question to create the quiz.');
return;
}
document.getElementById('quiz-creation').style.display = 'none';
document.getElementById('quiz-taking').style.display = 'block';
const quizQuestionsDiv = document.getElementById('quiz-questions');
quizQuestionsDiv.innerHTML = '';
questions.forEach((q, index) => {
const questionDiv = document.createElement('div');
questionDiv.classList.add('quiz-question');
const questionText = document.createElement('p');
questionText.textContent = `${index + 1}. ${q.question}`;
questionDiv.appendChild(questionText);
q.answers.forEach((answer, i) => {
const answerLabel = document.createElement('label');
answerLabel.textContent = answer;
const answerInput = document.createElement('input');
answerInput.type = 'radio';
answerInput.name = `question-${index}`;
answerInput.value = i;
questionDiv.appendChild(answerInput);
questionDiv.appendChild(answerLabel);
questionDiv.appendChild(document.createElement('br'));
});
quizQuestionsDiv.appendChild(questionDiv);
});
}
function submitQuiz() {
let score = 0;
questions.forEach((q, index) => {
const selectedAnswer = document.querySelector(`input[name="question-${index}"]:checked`);
if (selectedAnswer && selectedAnswer.value == q.correctAnswer) {
score++;
}
});
document.getElementById('quiz-taking').style.display = 'none';
document.getElementById('quiz-results').style.display = 'block';
const resultText = `You scored ${score} out of ${questions.length}`;
document.getElementById('result').textContent = resultText;
}
function resetQuiz() {
questions = [];
document.getElementById('quiz-creation').style.display = 'block';
document.getElementById('quiz-taking').style.display = 'none';
document.getElementById('quiz-results').style.display = 'none';
document.getElementById('quiz-form').reset();
}