forked from nastiazhyrnova/Quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.js
More file actions
118 lines (98 loc) · 3.46 KB
/
API.js
File metadata and controls
118 lines (98 loc) · 3.46 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Question from "./question.js";
import Quiz from "./quiz.js";
import App from "./app.js";
const importedQuestions = (_ => {
const mainFunction = async _ => {
//1.fetch API
const endPoint = `https://opentdb.com/`
const quantity = 10;
const difficulty = `easy`;
const type = `multiple`
const URL = `${endPoint}api.php?amount=${quantity}&difficulty=${difficulty}&type=${type}`
//function to shuffle
const shuffle = arr => {
let resultArr = [];
for (let i = arr.length; i > 0; i--) {
const randomIndex = Math.floor(Math.random() * i);
resultArr.push(arr[randomIndex]);
arr.splice(randomIndex, 1);
}
return resultArr;
}
//function to define a correct answer
const findAnswerKeyIndex = (answers, correct) => {
let key;
answers.forEach((elem, index) => {
if (elem === correct) {
key = index;
}
})
return key;
}
//function to create new question objects and push them to an array questionsArr
const renderImportedData = _ => {
let questionsArr = [];
importedData.results.forEach((elem, index) => {
const answers = shuffle(elem.incorrect_answers.concat(elem.correct_answer));
const answerKey = findAnswerKeyIndex(answers, elem.correct_answer);
const newQuestionObj = new Question (elem.question, answers, answerKey);
questionsArr.push(newQuestionObj);
});
return questionsArr;
}
//create new quiz
//functions
const request = await fetch(URL)
const importedData = await request.json();
const importedQuiz = await new Quiz(renderImportedData());
return importedQuiz;
}
return {
mainFunction
}
})();
export default importedQuestions;
//Questions list
// const q1 = new Question (
// "What is the earliest cave painting site found?",
// ["Altamira, Spain",
// "Chauvet, France",
// "Maros-Pangkep, Indonesia",
// "Lascaux, France"],
// 2
// );
// const q2 = new Question (
// "At the territory of which modern country is located ancient Babylon?",
// ["Iraq",
// "Syria",
// "Jordan",
// "Iran"],
// 0
// );
// const q3 = new Question (
// "Where famous Egyptian pyramids are located?",
// ["Karnak",
// "Giza",
// "Luxor",
// "Saqqara"],
// 1
// );
// const q4 = new Question (
// "Who was the first female emperor of Egypt?",
// ["Nefertiti",
// "Cleopatra",
// "Tusret",
// "Hatshepsut"],
// 3
// );
// const q5 = new Question (
// "According to the legend, who Knossos palace (Crete) was built for?",
// ["King Minos",
// "Minotaur",
// "Ariadne",
// "Aegeus"],
// 1
// );
//initialize the quiz
// const myQuiz = new Quiz([q1, q2, q3, q4, q5]);
// const myQuiz = mainFunction();