-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndex.js
159 lines (133 loc) · 4.42 KB
/
ndex.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
const ContainerOne = document.querySelector("#container-1"),
ContainerTwo = document.querySelector("#container-2"),
ContainerThree = document.querySelector("#container-3"),
QuestionNumber = document.querySelector("#ques-no"),
QuestionType = document.querySelector("#ques-type"),
TotalTime = document.querySelector("#time"),
StartBtn = document.querySelector("#submiting"),
CheckBtn = document.querySelector("#check"),
NextBtn = document.querySelector("#next"),
questionDiv = document.querySelector(".question-holder"),
CountUp = document.querySelector(".count-down"),
QuesNo = document.querySelector(".question-number"),
Options = document.querySelector(".options"),
TimeUsed = document.querySelector(".time-used"),
YourScore = document.querySelector(".your-score"),
TotalScore = document.querySelector(".total-score"),
PlayAgain = document.querySelector("#play-again");
const num = QuestionNumber.value;
const type = QuestionType.value;
const startMin = 0;
let timer = startMin * 60,
currentQuestion,
score;
function updateCounting() {
let minute = Math.floor(timer / 60);
let seconds = Number(timer % 60);
minute = minute < 10 ? "0" + minute : minute;
seconds = seconds < 10 ? "0" + seconds : seconds;
CountUp.innerHTML = `${minute}:${seconds}`;
TimeUsed.innerHTML = `${minute}:${seconds}`;
timer++;
}
StartBtn.addEventListener("click", StartGame);
function StartGame(e) {
e.preventDefault();
const num = QuestionNumber.value;
const type = QuestionType.value;
const categ = "17";
const url = `https://opentdb.com/api.php?amount=${num}&category=${categ}&difficulty=${type}&type=multiple`;
fetch(url)
.then((res) => res.json())
.then((data) => {
questions = data.results;
ContainerOne.classList.add("hide"), ContainerTwo.classList.remove("hide");
currentQuestion = 1;
score = 0;
showQuestion(questions[0]);
});
timing = setInterval(updateCounting, 1000);
console.log(num, type);
}
function showQuestion(ques) {
questionDiv.innerHTML = ques.question;
answers = [...ques.incorrect_answers, ques.correct_answer];
answers.sort(() => Math.random() - 0.5);
console.log(answers);
Options.innerHTML = "";
answers.forEach((answer) => {
Options.innerHTML += ` <span class="radio-con">
<span class="nego">${answer}</span>
</span>`;
});
QuesNo.innerHTML = `<p style="font-size: 14px; font-weight: 700">${
questions.indexOf(ques) + 1
}</p>/${questions.length}`;
const AnswerDiv = Options.querySelectorAll(".radio-con");
AnswerDiv.forEach((answer) => {
answer.addEventListener("click", () => {
if (!answer.classList.contains("selected")) {
AnswerDiv.forEach((answer) => {
answer.classList.remove("selected");
});
answer.classList.add("selected");
CheckBtn.style.opacity = 1;
}
});
});
}
CheckBtn.addEventListener("click", () => {
CheckAnswer();
});
CheckAnswer = () => {
const selectedOne = document.querySelector(".radio-con.selected");
const AllShow = document.querySelectorAll(".radio-con");
console.log("working");
if (selectedOne) {
const alert = selectedOne.querySelector(".nego").textContent;
CheckBtn.classList.add("hide");
NextBtn.classList.remove("hide");
if (alert == questions[currentQuestion - 1].correct_answer) {
selectedOne.classList.add("correct");
score++;
} else {
AllShow.forEach((show) => {
const ShowChild = show.querySelector(".nego");
if (
ShowChild.textContent == questions[currentQuestion - 1].correct_answer
) {
show.classList.add("correct");
}
});
selectedOne.classList.add("incorrect");
}
const answerAll = document.querySelectorAll(".radio-con");
answerAll.forEach((answer) => {
answer.classList.add("checked");
});
}
};
NextBtn.addEventListener("click", () => {
NextQuestion();
});
NextQuestion = () => {
if (currentQuestion < questions.length) {
currentQuestion++;
showQuestion(questions[currentQuestion - 1]);
CheckBtn.classList.remove("hide");
NextBtn.classList.add("hide");
console.log(score);
} else {
Endgame();
}
};
function Endgame() {
ContainerTwo.classList.add("hide");
ContainerThree.classList.remove("hide");
YourScore.innerHTML = score;
TotalScore.innerHTML = questions.length;
clearInterval(timing);
}
PlayAgain.addEventListener("click", () => {
window.location.reload();
});