-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion-generator.js
145 lines (118 loc) · 4.28 KB
/
question-generator.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var storageRef = firebase.storage().ref();
var ansDatabase = firebase.database();
var imgLoadedCounter = 0;
function randomiseQuestion(){
var table = document.getElementById("table");
var loader = document.getElementById('loader');
imgLoadedCounter = 0;
while (table.hasChildNodes()) {
table.removeChild(table.firstChild);
}
table.style.display = "none";
var numberField = document.getElementById("selectNumber");
var number = numberField.value;
var placeholder = document.getElementById("placeholder-text");
placeholder.style.display = "none";
loader.style.display = "block";
var toggleButton = document.getElementById("toggle-button");
toggleButton.style.display = "none";
var optionsText = document.getElementById("generation-line");
optionsText.style.width="950px";
var subjectList = document.getElementById("selectSubject");
var subject = subjectList.options[subjectList.selectedIndex].text;
var arr = [];
while(arr.length < number){
var randomNumber = 0;
if(subject!=="Economics"){
randomNumber = Math.floor((Math.random()*30) + 1);
} else {
randomNumber = Math.floor((Math.random()*89) + 1);
}
if(arr.indexOf(randomNumber) > -1) {continue;}
arr[arr.length] = randomNumber;
}
//window.alert(arr);
for(var i=0; i<arr.length; i++){
var randomisedNumber = arr[i];
var parentTD = "q"+i;
var incrementTD = "i"+i;
var parentTR = "tr"+i;
//window.alert("loop");
addQuestion(i+1, randomisedNumber, parentTD, parentTR, incrementTD, subject);
}
}
function addQuestion(increment, questionNo, parentTdId, parentTrId, incrementTdId, subject){
//setting display of answers buttons to block
var answersList = document.getElementById("answers-list");
answersList.style.display = "block";
//Creating table and child tr
var table = document.getElementById("table");
var tr = document.createElement("tr");
tr.setAttribute("id", parentTrId);
table.appendChild(tr);
//Creating child td and incrememnt td
var td = document.createElement("td");
td.setAttribute("id", parentTdId);
td.setAttribute("class", "question-td");
var itd = document.createElement("td");
itd.setAttribute("id", incrementTdId);
itd.setAttribute("class", "number-td");
itd.setAttribute("valign", "top");
tr.appendChild(itd);
tr.appendChild(td);
//Creating question images
var question = document.createElement("img");
question.setAttribute("id", subject+increment);
question.setAttribute("class", subject+"-question-img");
//Creating numbering on the side
var qnumber = document.createElement("p");
qnumber.setAttribute("class", "qnumber left-box");
qnumber.innerHTML = "Q"+increment;
//Creating Answer Text
var ans = document.createElement('p');
ans.setAttribute("class", "answer left-box");
ans.style.display = "none";
storageRef.child('Paper2/'+subject+'/'+subject.slice(0, 4)+questionNo+'.png').getDownloadURL().then(function(url){
//window.alert("function");
//window.alert(questionNo);
ansDatabase.ref(subject+"/"+subject.slice(0, 4)+questionNo).on('value', function(snapshot){
//window.alert(snapshot.val()+" "+questionNo);
ans.innerHTML = snapshot.val();
});
question.setAttribute("src", url);
question.setAttribute("onload", "loaded()");
});
td.appendChild(question);
itd.appendChild(qnumber);
itd.appendChild(ans);
}
function toggleAnswers(){
var answerElements = document.getElementsByClassName('answer');
var toggleButton = document.getElementById('toggle-button');
for (var i in answerElements) {
if (answerElements[i].style.display === "none") {
answerElements[i].style.display = 'block';
toggleButton.innerHTML = "Hide Answers";
} else {
answerElements[i].style.display = 'none';
toggleButton.innerHTML = "Show Answers";
}
}
}
function loaded(){
imgLoadedCounter+=1;
var numberField = document.getElementById("selectNumber");
var number = numberField.value;
if (imgLoadedCounter==number){
var table = document.getElementById("table");
var loader = document.getElementById('loader');
var toggleButton = document.getElementById('toggle-button');
loader.style.display = "none";
table.style.display = "table";
var optionsText = document.getElementById("generation-line");
optionsText.style.width="1200px";
toggleButton.style.display = "block";
} else {
return;
}
}