-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
177 lines (161 loc) · 6.45 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
177
// Initial References
const questions = [
{ image: "Alakazam.jpg", correct_option: "Alakazam" },
{ image: "Arcanine.jpg", correct_option: "Arcanine" },
{ image: "Bulbasaur.jpg", correct_option: "Bulbasaur" },
{ image: "Cubone.jpg", correct_option: "Cubone" },
{ image: "Ditto.jpg", correct_option: "Ditto" },
{ image: "Gloom.jpg", correct_option: "Gloom" },
{ image: "Gyarados.jpg", correct_option: "Gyarados" },
{ image: "Hitmonlee.jpg", correct_option: "Hitmonlee" },
{ image: "Horsea.jpg", correct_option: "Horsea" },
{ image: "Koffing.jpg", correct_option: "Koffing" },
{ image: "Mewtwo.jpg", correct_option: "Mewtwo" },
{ image: "Seaking.jpg", correct_option: "Seaking" },
{ image: "Tauros.jpg", correct_option: "Tauros" },
{ image: "Venonat.jpg", correct_option: "Venonat" },
{ image: "Victreebe.jpg", correct_option: "Victreebe" },
{ image: "eevee.jpg", correct_option: "Eevee" },
];
const optionsArray = [
"Alakazam", "Arcanine", "Bulbasaur", "Cubone", "Ditto", "Gloom",
"Gyarados", "Hitmonlee", "Horsea", "Koffing", "Mewtwo", "Pikachu",
"Seaking", "Tauros", "Venonat", "Victreebe", "eevee", "Ivysaur",
"Venusaur", "Charmander", "Charmeleon", "Charizard", "Squirtle",
"Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree", "Weedle",
"Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata",
"Raticate", "Spearow", "Fearow", "Ekans", "Arbok",
];
const container = document.querySelector(".container");
const gameContainer = document.querySelector(".game-container");
const startButton = document.getElementById("start");
const scoreContainer = document.querySelector(".score-container");
const userScore = document.getElementById("user-score");
let timer = document.getElementsByClassName("timer")[0];
let nextBtn;
let score, currentQuestion, finalQuestions;
let countdown,
count = 11;
// Function to generate random Pokemon colors
const generateRandomColor = () => {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
// Random value from array
const randomValueGenerator = (array) =>
array[Math.floor(Math.random() * array.length)];
// Random shuffle array
const randomShuffle = (array) => array.sort(() => 0.5 - Math.random());
// Start game
const startGame = () => {
scoreContainer.classList.add("hide");
gameContainer.classList.remove("hide");
finalQuestions = populateQuestions();
score = 0;
currentQuestion = 0;
cardGenerator(finalQuestions[currentQuestion]);
};
// Timer
const timerDisplay = () => {
countdown = setInterval(() => {
count -= 1;
timer.innerHTML = `<span>Time Left: </span>${count}s`;
if (count == 0) {
clearInterval(countdown);
nextQuestion();
}
}, 1000);
};
// Create options
const populateOptions = (correct_option) => {
let arr = [];
arr.push(correct_option);
let optionsCount = 1;
while (optionsCount < 4) {
let randomvalue = randomValueGenerator(optionsArray);
if (!arr.includes(randomvalue)) {
arr.push(randomvalue);
optionsCount += 1;
}
}
return arr;
};
// Choose random questions
const populateQuestions = () => {
let questionsCount = 0;
let chosenObjects = [];
let questionsBatch = [];
while (questionsCount < 5) {
let randomvalue = randomValueGenerator(questions);
let index = questions.indexOf(randomvalue);
if (!chosenObjects.includes(index)) {
questionsBatch.push(randomvalue);
chosenObjects.push(index);
questionsCount += 1;
}
}
return questionsBatch;
};
// Check selected answer
const checker = (e) => {
let userSolution = e.target.innerText;
let options = document.querySelectorAll(".option");
if (userSolution === finalQuestions[currentQuestion].correct_option) {
e.target.classList.add("correct");
score++;
} else {
e.target.classList.add("incorrect");
options.forEach((element) => {
if (element.innerText == finalQuestions[currentQuestion].correct_option) {
element.classList.add("correct");
}
});
}
clearInterval(countdown);
options.forEach((element) => {
element.disabled = true;
});
};
// Next question
const nextQuestion = () => {
currentQuestion += 1;
if (currentQuestion == finalQuestions.length) {
gameContainer.classList.add("hide");
scoreContainer.classList.remove("hide");
startButton.innerText = `Restart`;
userScore.innerHTML =
"Your score is " + score + " out of " + currentQuestion;
clearInterval(countdown);
} else {
cardGenerator(finalQuestions[currentQuestion]);
}
};
// Card UI
const cardGenerator = (cardObject) => {
const { image, correct_option } = cardObject;
let options = randomShuffle(populateOptions(correct_option));
const newBackgroundColor = generateRandomColor();
container.innerHTML = `<div class="quiz" style="background-color: ${newBackgroundColor};">
<p class="num">${currentQuestion + 1}/5</p>
<div class="questions">
<img class="pokemon-image" src="${image}" alt="Pokemon"/>
</div>
<div class="options">
<button class="option" onclick="checker(event)">${options[0]}</button>
<button class="option" onclick="checker(event)">${options[1]}</button>
<button class="option" onclick="checker(event)">${options[2]}</button>
<button class="option" onclick="checker(event)">${options[3]}</button>
</div>
<div class="nxt-btn-div">
<button class="next-btn" onclick="nextQuestion()">Next</button>
</div>
</div>`;
count = 11;
clearInterval(countdown);
timerDisplay();
};
startButton.addEventListener("click", startGame);