-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
49 lines (41 loc) · 1.66 KB
/
model.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
import { API_URL, QUES_PER_PAGE } from "./config.js";
import { getJSON, getRandomIndex } from "./helpers.js";
export const state = {
quizInfo: {},
questions: [],
curPage: 1,
questionPerPage: QUES_PER_PAGE,
point: 0,
}
export const setQuestions = async function (formData) {
try {
const [amount, category, difficulty, type] = formData.map(data => data[1] !== 'any' ? data[1] : '');
state.quizInfo = { amount, category, difficulty, type };
const infoData = state.quizInfo;
const urlLastPart = `${infoData.category !== '' ? `&category=${infoData.category}` : ''}${infoData.difficulty !== '' ? `&difficulty=${infoData.difficulty}` : ''}${infoData.type !== '' ? `&type=${infoData.type}` : ''}`;
const data = await getJSON(`${API_URL}amount=${infoData.amount}${urlLastPart}`);
state.questions = data.results.map((ques, i) => {
ques.incorrect_answers.splice(getRandomIndex(), 0, ques.correct_answer);
return {
questionId: i + 1,
question: ques.question,
correctAnswer: ques.correct_answer,
option: ques.incorrect_answers,
category: ques.category,
difficulty: ques.difficulty,
type: ques.type,
}
});
} catch (err) {
throw err;
}
}
export const getQuestionPerPage = function (page = state.curPage) {
state.curPage = page;
const start = (page - 1) * state.questionPerPage;
const end = page * state.questionPerPage;
return state.questions.slice(start, end);
}
export const setUserPoint = function (userPoin) {
state.point += userPoin;
}