-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.js
58 lines (48 loc) · 1.39 KB
/
runner.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
const params = {
questions: 5,
options: 4,
text: `1. What are the two main concepts that object-oriented programming languages that use classes support?
a) Procedures and variables
b) Classes and methods
c) Objects and instances
d) Data and functions
(answer: b)
2. What is the purpose of encapsulation in object-oriented programming?
a) To hide data from semantically related functions
b) To allow external code to access internal workings of an object `,
};
const generateQuestions = (params) => {
const questions = [];
const textLines = params.text.split('\n');
const qRegexStr = Array.from(
{ length: params.questions },
(v, i) => i + 1
).reduce((acc, curr, i) => acc + `(${curr}.*)`, '');
const qRegexp = new RegExp(qRegexStr, 'g');
const qData = {
title: '',
options: [],
answer: '',
};
textLines.forEach((_line) => {
const line = _line.trim();
const isQuestion = line.match(qRegexp) !== null;
const isOption = /^\w\)/.test(line);
if (isQuestion) {
qData.title = line.replace(/\d\.\s/g, '');
}
if (isOption) {
qData.options.push(line);
}
if (!isQuestion && !isOption && line) {
qData.answer = line;
}
if (!line) {
questions.push(qData);
qData.title = '';
qData.options = [];
qData.answer = '';
}
});
};
// console.log(structureOpenAiResponse(params));