-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
58 lines (50 loc) · 1.84 KB
/
util.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
module.exports = {
// Takes a subject and two arrays of questions and answers that the host types in, and writes them to a JSON file
write_to_JSON: function(json_object, subject, questions, correct_answers)
{
const fs = require('fs');
for(let i = 0; i < questions.length; i++)
{
let question = {
"type": "fill_in_the_blank",
"case_sensitive": false,
"text": questions[i],
"correct_answer": correct_answers[i],
"subject": subject
}
json_object.questions.push(question);
}
let json_string = JSON.stringify(json_object);
fs.writeFile("./questions.json", json_string, err => {
if(err) {
console.error(err);
}
});
},
// Called when the host chooses which set of questions they want to use. Returns an array containing the questions.
get_questions: function(json_object, chosen_subject)
{
let questions = [];
for(let i = 0; i < json_object.questions.length; i++)
{
if(json_object.questions[i].subject == chosen_subject)
{
questions.push(json_object.questions[i].text);
}
}
return questions;
},
// Called when the host chooses which set of questions they want to use. Returns an array containing the correct answers.
get_correct_answers: function(json_object, chosen_subject)
{
let answers = [];
for(let i = 0; i < json_object.questions.length; i++)
{
if(json_object.questions[i].subject == chosen_subject)
{
answers.push(json_object.questions[i].correct_answer);
}
}
return answers;
}
};