-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
46 lines (40 loc) · 1.99 KB
/
quiz.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
// Fetch quiz questions from API
fetch('https://opentdb.com/api.php?amount=10&type=multiple')
.then(response => response.json())
.then(data => {
// Get quiz container
const quizContent = document.getElementById('quiz-content');
// Loop over questions
data.results.forEach((question, index) => {
// Create a div for the question
const questionDiv = document.createElement('div');
questionDiv.className = 'mb-4';
// Create the question label
const questionLabel = document.createElement('label');
questionLabel.innerHTML = question.question;
questionLabel.className = 'd-block mb-2';
questionDiv.appendChild(questionLabel);
// Shuffle answers
const answers = question.incorrect_answers;
const correctAnswerIndex = Math.floor(Math.random() * (answers.length + 1));
answers.splice(correctAnswerIndex, 0, question.correct_answer);
// Loop over answers
answers.forEach(answer => {
// Create the answer input and label
const answerInput = document.createElement('input');
answerInput.type = 'radio';
answerInput.name = `question${index}`;
answerInput.value = answer === question.correct_answer ? 1 : 0;
answerInput.className = 'mr-2';
const answerLabel = document.createElement('label');
answerLabel.appendChild(answerInput);
answerLabel.innerHTML += answer;
// Add the answer to the question div
questionDiv.appendChild(answerLabel);
questionDiv.appendChild(document.createElement('br'));
});
// Add the question div to the quiz content
quizContent.appendChild(questionDiv);
});
})
.catch(error => console.error(error));