-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_loader.js
More file actions
41 lines (40 loc) · 1.48 KB
/
quiz_loader.js
File metadata and controls
41 lines (40 loc) · 1.48 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
// quiz_loader.js
// Loads questions from questions.txt and provides them to the quiz app
async function loadQuestionsFromTxt(url) {
const response = await fetch(url);
if (!response.ok) throw new Error('Не удалось загрузить файл вопросов');
const text = await response.text();
const lines = text.split(/\r?\n/).filter(line => line.trim() && !line.startsWith('#'));
const questions = [];
function splitEscaped(str, sep) {
let arr = [];
let cur = '';
let escape = false;
for (let i = 0; i < str.length; ++i) {
const c = str[i];
if (escape) {
cur += c;
escape = false;
} else if (c === '\\') {
escape = true;
} else if (c === sep) {
arr.push(cur);
cur = '';
} else {
cur += c;
}
}
arr.push(cur);
return arr;
}
for (const line of lines) {
const parts = splitEscaped(line, '|');
if (parts.length !== 3) continue;
const question = parts[0].replace(/\\\|/g, '|');
const variants = splitEscaped(parts[1], ';').map(v => v.replace(/\\\|/g, '|'));
const correct = parseInt(parts[2], 10);
if (isNaN(correct) || correct < 0 || correct >= variants.length) continue;
questions.push({ question, variants, correct });
}
return questions;
}