-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
176 lines (162 loc) · 4.6 KB
/
script.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//create mode
//start game
//next button
//answer determiner
//timing function
//DOM Elements
const startButton = document.getElementById("start");
const questionContainer = document.querySelector(".question-container");
const questionContent = document.querySelector(".question-box");
const answerContent = document.querySelector(".answer-box");
const nextButton = document.getElementById("next");
const gameOver = document.querySelector(".game-over");
const restartButton = document.getElementById("game-over-btn");
const container = document.querySelector(".container");
const modeContainer = document.querySelector(".mode-container");
const mode = Array.from(document.querySelectorAll(".mode-selector"));
const loader = document.getElementById("loader");
//Declaration of variables
let currentQuestion = {};
let questionCounter = 0;
let availableQuestions = [];
let shuffledQuestions, questionIndex, acceptingAnswers;
//Event Listeners
startButton.addEventListener("click", selectMode);
nextButton.addEventListener("click", () => {
setQuestion();
});
restartButton.addEventListener("click", () => {
gameOver.classList.add("hide");
container.classList.add("hide");
selectMode();
});
//select Mode
function selectMode() {
startButton.classList.add("hide");
modeContainer.classList.remove("hide");
mode.forEach((modal) => {
modal.addEventListener("click", startMode);
});
}
//start Mode
function startMode(e) {
let modeName = "";
const button = e.target.innerHTML;
switch (button) {
case "Easy":
modeName = "easy";
startGame(modeName);
break;
case "Medium":
modeName = "medium";
startGame(modeName);
break;
case "Hard":
modeName = "hedium";
startGame(modeName);
break;
case "Survival":
modeName = "";
startGame(modeName);
break;
}
}
//start Game
function startGame(m) {
modeContainer.classList.add("hide");
loader.classList.remove("hide");
// console.log('began')
fetch(
`https://the-trivia-api.com/api/questions?categories=general_knowledge,arts_and_literature,science,sport_and_leisure,soceity_and_culture&limit=50&fifficulty=${m}`
)
.then((response) => {
return response.json();
})
.then((data) => {
data.map((d) => {
let answerposition = Math.floor(Math.random() * 4);
d.answerNumber = answerposition;
const answers = d.incorrectAnswers;
answers.splice(answerposition, 0, d.correctAnswer);
});
questionCounter = 0;
availableQuestions = [...data];
setQuestion();
});
}
//set question
function setQuestion() {
resetContainer();
questionCounter++;
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionIndex];
showQuestion(currentQuestion);
loader.classList.add("hide");
container.classList.remove("hide");
availableQuestions.splice(questionIndex, 1);
}
//reset Container
function resetContainer() {
nextButton.classList.add("hide");
while (answerContent.firstChild) {
answerContent.removeChild(answerContent.firstChild);
}
}
//show Question
function showQuestion(q) {
let count = 0;
questionContent.innerHTML = q.question;
const newAnswers = q.incorrectAnswers;
newAnswers.forEach((newAnswer) => {
const a = document.createElement("button");
a.dataset.number = count;
count++;
a.classList.add("answer");
a.innerHTML = newAnswer;
answerContent.appendChild(a);
a.addEventListener("click", selectAnswer);
const comparer = a.dataset.number;
const decider = currentQuestion.answerNumber;
if (comparer == decider) {
a.dataset.correct = true;
}
});
}
//select Answer
function selectAnswer(e) {
const button = e.target;
const rightAns = button.dataset.correct;
setStatus(document.body, rightAns);
Array.from(answerContent.children).forEach((child) => {
setStatus(child, child.dataset.correct);
child.removeEventListener("click", selectAnswer);
});
if (availableQuestions.length > 0) {
nextButtonFunction();
} else {
startButton.innerHTML = "Restart";
startButton.classList.remove("hide");
}
}
//set Status
function setStatus(element, status) {
clearStatus(element);
if (status) {
element.classList.add("correct");
} else {
element.classList.add("wrong");
}
}
//clear Status
function clearStatus(element) {
element.classList.remove("correct");
element.classList.remove("wrong");
}
//nextQuestion
function nextButtonFunction() {
if (document.body.classList.contains("correct")) {
nextButton.classList.remove("hide");
} else {
gameOver.classList.remove("hide");
}
}