-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
101 lines (90 loc) · 3.43 KB
/
Copy pathjavascript.js
File metadata and controls
101 lines (90 loc) · 3.43 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
//Store correct answers
var correctAnswers = {
"q1": "Answer2",
"q2": "Answer1",
"q3": "Answer1",
"q4": "Answer2",
"q5": "Answer4",
"q6": "Answer2",
"q7": "Answer4",
"q8": "Answer3",
"q9": "Answer3",
"q10": "Answer1"
};
//Function to calculate the score
function calculateScore() {
var totalScore = 0;
var answeredQuestions = 0;
//Loop through each question and retrieve answers
for (var i = 1; i <= 10; i++) {
var userAnswer = localStorage.getItem("q" + i);
//Check if question was answered
if (userAnswer) {
answeredQuestions++; //Increment answered questions
//Check if the users answer matches the correct answer
if (userAnswer === correctAnswers["q" + i]) {
totalScore++; //Increment total score if correct
}
}
}
return { totalScore: totalScore, answeredQuestions: answeredQuestions };
}
//Function to display the final score
function displayFinalScore(score) {
var finalScoreElement = document.getElementById("final-score");
if (finalScoreElement) {
finalScoreElement.textContent = "Your final score: " + score.totalScore + " out of 10";
}
}
//Function to save the selected answer to localStorage
function saveAnswer(questionId, answer) {
localStorage.setItem(questionId, answer);
}
//Function to check if any radio button for a specific question is checked
function isAnyRadioButtonChecked(questionId) {
var radioButtons = document.querySelectorAll('input[name="' + questionId + '"]');
for (var i = 0; i < radioButtons.length; i++) {
if (radioButtons[i].checked) {
return true;
}
}
return false;
}
document.addEventListener("DOMContentLoaded", function() {
//Added onchange event listener to all radio buttons to save the selected answers as it wasnt doing it without it
var radioButtons = document.querySelectorAll('input[type="radio"]');
radioButtons.forEach(function(radioButton) {
radioButton.addEventListener("change", function() {
var questionId = this.name;
var answer = this.value;
saveAnswer(questionId, answer);
});
});
//Add event listeners to next question links to make sure user gives answer
var nextQuestionLinks = document.querySelectorAll(".next-question");
nextQuestionLinks.forEach(function(link) {
link.addEventListener("click", function(event) {
var currentQuestion = this.getAttribute("data-question");
if (!isAnyRadioButtonChecked(currentQuestion)) {
event.preventDefault(); //Prevent following the link
alert("Please select an answer before proceeding to the next question.");
}
});
});
//Add event listener for the submit button (different for the last question)
var submitButton = document.getElementById("submit-btn");
if (submitButton) {
submitButton.addEventListener("click", function() {
if (!isAnyRadioButtonChecked("q10")) {
alert("Please select an answer before submitting.");
} else {
//Calculate the score
var finalScore = calculateScore();
//Display the final score
displayFinalScore(finalScore);
//Redirect to the final score page
window.location.href = "Final_Score.html";
}
});
}
});