-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
37 lines (28 loc) · 972 Bytes
/
utils.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
const questions = require('./questions.json');
const { Random } = require('random-js');
const getRandomQuestion = (topic) => {
const random = new Random();
let questionTopic = topic.toLowerCase();
if (questionTopic === 'случайный вопрос') {
questionTopic =
Object.keys(questions)[
random.integer(0, Object.keys(questions).length - 1)
];
}
const randomQuestionIndex = random.integer(
0,
questions[questionTopic].length - 1,
);
return {
question: questions[questionTopic][randomQuestionIndex],
questionTopic,
};
};
const getCorrectAnswer = (topic, id) => {
const question = questions[topic].find((question) => question.id === id);
if (!question.hasOptions) {
return question.answer;
}
return question.options.find((option) => option.isCorrect).text;
};
module.exports = { getRandomQuestion, getCorrectAnswer };