forked from alexgreene/WikiQuiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
157 lines (134 loc) · 4.39 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
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
var article_name = document.getElementById('article_name');
var question_body = document.getElementById('question_body');
var search = document.getElementById('article_name_input');
var next_question_btn = document.getElementById('next_question_btn');
var answer_a = document.getElementById('answer_a');
var answer_b = document.getElementById('answer_b');
var answer_c = document.getElementById('answer_c');
var answer_response_label = document.getElementById('answer_response_label');
answer_response_label.style.display = "none";
answer_a.style.display = "none";
answer_b.style.display = "none";
answer_c.style.display = "none";
next_question_btn.style.display = "none";
var data;
var q_idx = 0;
var curQuery;
var correct_answer;
function new_article_query(_data) {
if (!_data) {
make_request(search.value, new_article_query);
curQuery = search.value;
} else {
article_name.innerHTML = search.value;
data = _data;
//console.log(data);
load_question();
}
}
// asynch GET request to server.py
function make_request(query, _callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
_callback(JSON.parse(xmlHttp.responseText));
}
}
xmlHttp.open("GET", 'http://127.0.0.1:5000/quiz/' + query + "/", true);
xmlHttp.send(null);
}
function load_question() {
loadUIForNextQuestion()
if (q_idx === 10) {
q_idx = 0;
make_request(curQuery, new_article_query);
} else {
label = data['questions'][q_idx][0];
question_body.innerHTML = convert_to_redacted(
data['questions'][q_idx][1],
data['questions'][q_idx][2],
label);
answers = [];
correct_answer = data['questions'][q_idx][2];
answers.push(correct_answer);
answers.push(get_wrong_answer(label));
answers.push(get_wrong_answer(label));
shuffle(answers);
answer_a.innerHTML = answers[0];
answer_b.innerHTML = answers[1];
answer_c.innerHTML = answers[2];
q_idx += 1;
}
}
function get_wrong_answer(label) {
if (label === "LOCATION") {
return randomFromArr(data['locations']);
} else if (label === "PROPER") {
return randomFromArr(data['propers']);
} else {
return randomFromArr(data['numbers']);
}
}
// extracts a random element from an array
// logic taken from Stack Overflow answer
function randomFromArr(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
// Hides the correct answer from the question text and
// replaces it with a ? emoji
function convert_to_redacted(text, answer, label) {
_answer = answer;
// when handling locations, the parser reconstructs with comma
// spaced from prior word ie IN TAKANEZAWA , JAPAN
// rather than IN TAKANEZAWA, JAPAN
_answer = answer.replace(' ,',',');
if (label === "NUMBER") {
// if the answer is a number, we want to match
// the numerical portion, not $, %, etc
_answer = answer.replace(/[^0-9]/g,'');
// if the the number is spelled out
// ie. `twelve`, leave it as it is.
if (_answer === "") {
_answer = answer;
}
}
return text.replace(_answer, "❓");
}
function handleAnswerResponse(answer_given) {
if (correct_answer === answer_given){
answer_response_label.innerHTML = "Correct!";
} else {
answer_response_label.innerHTML = "That's wrong...";
}
answer_response_label.style.display = "block";
}
function answered_a() {
handleAnswerResponse(answer_a.innerHTML);
}
function answered_b() {
handleAnswerResponse(answer_b.innerHTML);
}
function answered_c() {
handleAnswerResponse(answer_c.innerHTML);
}
function loadUIForNextQuestion() {
answer_response_label.style.display = "none";
answer_a.style.display = "block";
answer_b.style.display = "block";
answer_c.style.display = "block";
next_question_btn.style.display = "block";
}
// I borrowed the following from a Stack Overflow answer
/**
* Shuffles array in place.
* @param {Array} a items The array containing the items.
*/
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i--) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}