-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz.js
More file actions
172 lines (151 loc) · 5.14 KB
/
quiz.js
File metadata and controls
172 lines (151 loc) · 5.14 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var quizNumber = 1;
var questionNumber = 0;
var error = false;
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const email = urlParams.get('email');
window.onload = getQuestion(quizNumber, questionNumber);
function getQuestion(quizNum, questionNum) {
var url = "http://localhost:8085/api/quiz/1/";
// Uncomment next line for localhost testing
// url = "http://localhost:8085/api/person/";
// set options for cross origin header request
const options1 = {
method: "GET", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "default", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
},
};
// fetch the API
fetch(url, options1)
// response is a RESTful "promise" on any successful fetch
.then((response) => {
// check for response errors and display
if (response.status !== 200) {
const errorMsg = "Database response error: " + response.status;
console.log(errorMsg);
const tr = document.createElement("tr");
const td = document.createElement("td");
td.innerHTML = errorMsg;
tr.appendChild(td);
resultContainer.appendChild(tr);
return;
}
// valid response will contain json data
response.json().then((data) => {
console.log(data);
document.getElementById("quizNum").innerHTML = quizNumber;
document.getElementById("question").innerHTML =
"Question " +
data[questionNumber].questionNumber +
"/10" +
": " +
data[questionNumber].question;
//quizOptions
var quizOptions = data[questionNumber].quizOptions;
console.log("quizOptions = " + quizOptions);
var container = document.getElementById("options");
container.innerHTML="";
for (var i = 0; i < quizOptions.length; i++) {
var labelString = quizOptions[i];
var div = document.createElement("div");
var radio = document.createElement("input");
radio.type = "radio";
radio.name = "q"; // set the name attribute to group the radio buttons together
radio.value = labelString.substring(0,1);
// Create a label element for the radio button
var label = document.createElement("label");
label.appendChild(document.createTextNode(labelString)); // set the label text
// Add the radio button and label elements to the container element
container.appendChild(div);
container.appendChild(radio);
container.appendChild(label);
}
});
});
}
function nextQuestion() {
//Save the answer to the database
submitAnswer(quizNumber, questionNumber);
if(error){
return;
}
questionNumber = questionNumber + 1;
if(questionNumber == 10){
document.getElementById('nextBtn').value ='Results';
window.location = "http://localhost:4002/quizResults";
} else {
getQuestion(quizNumber, questionNumber);
}
}
function submitAnswer(quizNum, questionNum) {
error = false;
var url = "http://localhost:8085/api/quiz/submit";
// Uncomment next line for localhost testing
// url = "http://localhost:8085/api/person/";
/*
{
"quizNumber":"1",
"questionNumber":"1",
"answer":"A",
"email":"test2@test.com"
}
*/
console.log("QuizNumber" + quizNum);
console.log("QuestionNum" + (questionNum + 1));
var options = document.getElementsByName("q");
var answer ="";
for (var i = 0; i < options.length; i++) {
if (options[i].checked) {
answer = options[i].value;
//return;
}
}
document.getElementById('errors').innerHTML = '';
if(answer.trim() == '') {
error = true;
document.getElementById('errors').innerHTML = 'Please select answer';
return;
}
// Set body to include login data
const body = {
quizNumber: quizNum,
questionNumber: questionNum + 1,
answer: answer,
email: email,
};
// Set Headers to support cross origin
const requestOptions = {
method: "POST",
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "include", // include, *same-origin, omit
body: JSON.stringify(body),
headers: {
"content-type": "application/json",
},
};
// fetch the API
fetch(url, requestOptions)
// response is a RESTful "promise" on any successful fetch
.then((response) => {
// check for response errors and display
if (response.status !== 200) {
const errorMsg = "Database response error: " + response.status;
console.log(errorMsg);
const tr = document.createElement("tr");
const td = document.createElement("td");
td.innerHTML = errorMsg;
tr.appendChild(td);
resultContainer.appendChild(tr);
return;
}
// valid response will contain json data
response.json().then((data) => {
console.log(data);
});
});
}