-
Notifications
You must be signed in to change notification settings - Fork 0
/
testroom.js
64 lines (54 loc) · 2.03 KB
/
testroom.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
const questions = [
{
question: "What is the capital of France?",
choices: ["Paris", "Rome", "London"],
correctAnswer: "Paris"
},
{
question: "Which planet is known as the Red Planet?",
choices: ["Mars", "Venus", "Jupiter"],
correctAnswer: "Mars"
},
{
question: "What is the largest mammal?",
choices: ["Elephant", "Blue Whale", "Giraffe"],
correctAnswer: "Blue Whale"
},
// Add more questions here
// Add more questions here
];
const questionsDiv = document.getElementById('questions');
const submitButton = document.getElementById('submitQuiz');
const resultsDiv = document.getElementById('results');
// Create questions and answer choices
questions.forEach((q, index) => {
const questionDiv = document.createElement('div');
questionDiv.classList.add('question');
const questionText = document.createElement('p');
questionText.textContent = `${index + 1}. ${q.question}`;
questionDiv.appendChild(questionText);
q.choices.forEach(choice => {
const label = document.createElement('label');
const input = document.createElement('input');
input.type = 'radio';
input.name = `q${index}`;
input.value = choice;
label.appendChild(input);
label.appendChild(document.createTextNode(` ${choice}`));
questionDiv.appendChild(label);
});
questionsDiv.appendChild(questionDiv);
});
submitButton.addEventListener('click', showResults);
function showResults() {
let correctCount = 0;
questions.forEach((q, index) => {
const selectedAnswer = document.querySelector(`input[name="q${index}"]:checked`);
if (selectedAnswer && selectedAnswer.value === q.correctAnswer) {
correctCount++;
}
});
const totalQuestions = questions.length;
const percentageCorrect = (correctCount / totalQuestions) * 100;
resultsDiv.textContent = `You got ${correctCount} out of ${totalQuestions} questions correct (${percentageCorrect.toFixed(2)}%)`;
}