-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
executable file
·42 lines (34 loc) · 1.06 KB
/
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
38
39
40
41
42
function randomQuestion(questions) {
return questions[Math.floor(Math.random() * questions.length)];
}
function randomQuestionFunc(questions, n) {
const limit = questions.length < n ? questions.length : n;
const randomIndicesSet = new Set();
while (randomIndicesSet.size < limit) {
const randomIndex = Math.floor(Math.random() * questions.length);
if (!randomIndicesSet.has(randomIndex)) {
randomIndicesSet.add(randomIndex);
}
}
return Array.from(randomIndicesSet).map((randomIndex) => {
return questions[randomIndex];
});
}
function sortByCategory(questions, category) {
const filtered = questions.filter(
(question) => question.category.toLowerCase() === category
);
return filtered;
}
function sortByDifficulty(questions, difficulty) {
const filtered = questions.filter(
(question) => question.difficulty.toLowerCase() === difficulty
);
return filtered;
}
module.exports = {
randomQuestionFunc,
randomQuestion,
sortByCategory,
sortByDifficulty,
};