-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexam.js
More file actions
124 lines (121 loc) · 5.34 KB
/
exam.js
File metadata and controls
124 lines (121 loc) · 5.34 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const selectedCourse = localStorage.getItem("selectedCourse");
const selectedQuestionPaper = localStorage.getItem("selectedQuestionPaper");
const heading=document.getElementById("main-heading");
const navBar=document.getElementById("navBar")
const answers=[];
const selectionSound= new Audio("Audio\\selection.mp3");
const scrollSound= new Audio("Audio\\scroll.mp3");
let correctAnswers=0;
let wrongAnswers=0;
let choices=new Array(100).fill(null);
heading.textContent=selectedCourse+" "+selectedQuestionPaper;
function loadQuestionPaper(selectedCourse,selectedQuestionPaper){
const newForm=document.querySelector("form");
async function fetchData() {
try{
const response=await fetch(`Question Papers\\Data\\${selectedQuestionPaper}.json`);
if(!response.ok){
throw new Error(`HTTP error! status: ${response.status}`);
}
const data=await response.json();
return data;
}
catch(error){
console.error('Fetch Error:',error);
return null;
}
}
fetchData().then(data=>{
data.forEach(questionNumber=>{
const navButton=document.createElement("a");
navButton.href="#"+questionNumber.number;
navButton.textContent=questionNumber.number;
navBar.appendChild(navButton);
const newDiv=document.createElement("div");
newDiv.className="Question-Block";
newDiv.id=questionNumber.number;
const newQuestion=document.createElement("h2");
newQuestion.innerHTML=questionNumber.number+". "+questionNumber.question;
newDiv.appendChild(newQuestion);
if(questionNumber.figure){
const newFig=document.createElement("img");
newFig.src=`Figures\\${selectedQuestionPaper}-${questionNumber.number}.png`;
newFig.style.height="100px";
newFig.style.width="200px";
newFig.style.paddingLeft="50px";
newFig.style.pointerEvents="none";
newDiv.appendChild(newFig);
}
answers.push(questionNumber.answer);
questionNumber.options.forEach(option=>{
const newLabel=document.createElement("label");
newLabel.textContent=option;
newDiv.appendChild(newLabel);
if(questionNumber.diagrams){
const newDiagram=document.createElement("img");
newDiagram.src=`Diagrams\\${selectedQuestionPaper}-${questionNumber.number}-${option}.png`;
newDiagram.style.height="100px";
newDiagram.style.width="200px";
newDiagram.style.paddingLeft="50px";
newDiagram.style.pointerEvents="none";
newLabel.appendChild(newDiagram);
}
newDiv.appendChild(document.createElement("br"));
})
newForm.appendChild(newDiv);
const hint=document.createElement("div");
hint.className="hint";
hint.textContent="💡"+questionNumber.answer;
newDiv.appendChild(hint);
})
const submitButton=document.createElement("button");
submitButton.type="submit";
submitButton.textContent="SUBMIT";
submitButton.addEventListener('click',e=>{
e.preventDefault();
for(let i=0;i<100;i++){
if(choices[i]===answers[i]){
correctAnswers++;
}
}
localStorage.setItem("choices",JSON.stringify(choices));
localStorage.setItem("answers",JSON.stringify(answers));
localStorage.setItem("correctAnswers",correctAnswers)
localStorage.setItem("selectedCourse",selectedCourse);
localStorage.setItem("selectedQuestionPaper",selectedQuestionPaper);
window.open("Result.html",'_self');
});
newForm.appendChild(submitButton);
document.querySelectorAll(".Question-Block").forEach(question=>{
question.addEventListener("click",function(e){
if(e.target.tagName==="LABEL"){
selectionSound.play();
const clickedLabel=e.target;
const clickedParent=clickedLabel.parentNode;
clickedParent.querySelectorAll("label.chosen").forEach(label=>{
if(label!==clickedLabel){
label.classList.remove("chosen")
}
});
clickedLabel.classList.toggle("chosen");
const numbers=document.querySelectorAll('a');
numbers.forEach(scroll=>{
scroll.addEventListener('click',()=>scrollSound.play());
});
document.querySelectorAll('a').forEach(a=>{
if(a.textContent===clickedParent.id){
if(clickedLabel.classList.contains("chosen")){
a.classList.add("answered");
}
else{
a.classList.remove("answered");
}
}
});
choices[clickedParent.id-1]=clickedLabel.textContent;
}
})
})
})
}
loadQuestionPaper(selectedCourse,selectedQuestionPaper);