-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
210 lines (152 loc) · 4.86 KB
/
game.js
File metadata and controls
210 lines (152 loc) · 4.86 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
function game() {
//selecting the question
const question = document.getElementById("question");
//Take the choices and then convert them into an array to perform different actions on them
const choices = Array.from(document.getElementsByClassName("choice-text"));
let currentquestion = {};
// to add a delay for the next question
let acceptingAnswers = true;
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
let questions = [
{
question: "What are Smart wallets?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
},
{
question: "What is the difference between a testnet and a mainnet ?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
},
{
question: "Which of the following are a testnet?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "How do you put a project on chain?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "What are rollups?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "What is a chain fork?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "What does BIP stand for?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "How are onchain transactions different from offchain transactions?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "Which of the below is an off chain transaction?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
,
{
question: "What is a block in a blockchain?",
option1: "m",
option2: "n",
option3: "o",
option4: "p",
answer: 1
}
];
const correct_answer = 10;
const max_questions = 10;
InitGame = () => {
questionCounter = 1; // To keep record of the number of questions
score = 0;
availableQuestions = [...questions]; // To keep track of the total number of available questions
getNewQuestion();
};
getNewQuestion = () => {
// just in case you have hardcoded more than 10 but you want the user to answer only 10
if (availableQuestions.length === 0 || questionCounter >= max_questions) {
return window.location.assign("/end.html");
}
// To update and show the current number of the question we will picking up the class
document.querySelector(".hud-text").innerHTML = questionCounter++;
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
currentquestion = availableQuestions[questionIndex];
question.innerText = currentquestion.question;
choices.forEach(choice => {
const number = choice.dataset["number"];
choice.innerText = currentquestion["choice" + number];
});
availableQuestions.splice(questionIndex, 1);
acceptingAnswers = true;
};
choices.forEach(choice => {
choice.addEventListener("click", e => {
if (!acceptingAnswers) return;
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
//To mark the answer as correct or incorrect
const classtoapply = selectedAnswer == currentquestion.answer ? 'correct' : 'incorrect'
selectedChoice.parentElement.classList.add(classtoapply);
//Updating the score
if (selectedAnswer == currentquestion.answer) {
score++;
document.querySelector("#score").innerHTML = score;
}
setTimeout(() => {
selectedChoice.parentElement.classList.remove(classtoapply);
getNewQuestion();
}, 1000)
});
});
InitGame();
}
function redirectToGame() {
// Redirect to the game page
window.location.href = "game.html"; // Actual path to the game page
}
game();