-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (58 loc) · 2.41 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
document.addEventListener("DOMContentLoaded", function() {
const questionsContainer = document.getElementById("questionsContainer");
const examForm = document.getElementById("examForm");
const resultContainer = document.getElementById("resultContainer");
const scoreText = document.getElementById("scoreText");
// Define the exam questions
const questions = [
{
question: "What is the capital of France?",
options: ["Paris", "London", "Berlin", "Madrid"],
answer: "Paris"
},
{
question: "What is the square root of 16?",
options: ["2", "4", "6", "8"],
answer: "4"
},
{
question: "Who wrote the play 'Romeo and Juliet'?",
options: ["Shakespeare", "Hemingway", "Fitzgerald", "Twain"],
answer: "Shakespeare"
}
];
// Display questions on the page
questions.forEach((question, index) => {
const questionDiv = document.createElement("div");
questionDiv.classList.add("question");
const questionText = document.createElement("h3");
questionText.textContent = question.question;
questionDiv.appendChild(questionText);
question.options.forEach((option, optionIndex) => {
const optionLabel = document.createElement("label");
const optionInput = document.createElement("input");
optionInput.type = "radio";
optionInput.name = "question" + index;
optionInput.value = option;
optionLabel.appendChild(optionInput);
optionLabel.innerHTML += option;
questionDiv.appendChild(optionLabel);
});
questionsContainer.appendChild(questionDiv);
});
// Handle form submission
examForm.addEventListener("submit", function(event) {
event.preventDefault();
// Calculate the score
let score = 0;
questions.forEach((question, index) => {
const selectedOption = document.querySelector(
'input[name="question' + index + '"]:checked'
);
if (selectedOption && selectedOption.value.toLowerCase() === question.answer.toLowerCase()) {
score++;
}
});
// Display the score
scoreText.textContent = "Your score: " + score + "/" + questions.length;
resultContainer.style.display = "block";