Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
217 lines (192 loc) · 5.73 KB
/
Copy pathscript.js
File metadata and controls
217 lines (192 loc) · 5.73 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
208
209
210
211
212
213
214
215
216
217
function generateSquareQuestion() {
const num = Math.floor(Math.random() * 32) + 1;
return {
question: `${num}²`,
answer: num * num,
};
}
function generateCubeQuestion() {
const num = Math.floor(Math.random() * 12) + 1;
return {
question: `${num}³`,
answer: num * num * num,
};
}
function generateAdditionQuestion(steps = 3, maxDigits = 4) {
const numbers = Array.from({ length: steps }, () =>
Math.floor(Math.random() * Math.pow(10, maxDigits))
);
return {
question: numbers.join(" + "),
answer: numbers.reduce((a, b) => a + b, 0),
};
}
function generateSubtractionQuestion(steps = 3, maxDigits = 4) {
const numbers = Array.from({ length: steps }, () =>
Math.floor(Math.random() * Math.pow(10, maxDigits))
);
numbers.sort((a, b) => b - a); // biggest first
const question = numbers.join(" - ");
const answer = numbers.reduce((a, b) => a - b);
return { question, answer };
}
function generateMultiplicationQuestion(steps = 2, maxDigits = 3) {
const numbers = Array.from(
{ length: steps },
() => Math.floor(Math.random() * Math.pow(10, maxDigits - 1)) + 1
);
const question = numbers.join(" × ");
const answer = numbers.reduce((a, b) => a * b);
return { question, answer };
}
function generateDivisionQuestion(steps = 2, maxDigits = 3) {
let start = Math.floor(Math.random() * Math.pow(10, maxDigits - 1)) + 2;
let result = start;
let divisors = [start];
for (let i = 1; i < steps; i++) {
let divisor = Math.floor(Math.random() * 9) + 2;
result *= divisor;
divisors.push(divisor);
}
const question = [result, ...divisors.slice(1).reverse()].join(" ÷ ");
const answer = start;
return { question, answer };
}
function generateTableQuestion() {
const tableOf = Math.floor(Math.random() * 20) + 1; // 1 to 20
const multiplier = Math.floor(Math.random() * 10) + 1; // 1 to 10
return {
question: `${tableOf} × ${multiplier}`,
answer: tableOf * multiplier,
};
}
async function runQuiz() {
const totalQuestions = 10;
let score = 0;
const questionTypes = [
"square",
"cube",
"addition",
"subtraction",
"multiplication",
"division",
"table",
];
const startTime = Date.now();
for (let i = 0; i < totalQuestions; i++) {
const type =
questionTypes[Math.floor(Math.random() * questionTypes.length)];
let currentQuestion;
switch (type) {
case "square":
currentQuestion = generateSquareQuestion();
break;
case "cube":
currentQuestion = generateCubeQuestion();
break;
case "addition":
currentQuestion = generateAdditionQuestion();
break;
case "subtraction":
currentQuestion = generateSubtractionQuestion();
break;
case "multiplication":
currentQuestion = generateMultiplicationQuestion();
break;
case "division":
currentQuestion = generateDivisionQuestion();
break;
case "table":
currentQuestion = generateTableQuestion();
break;
}
const userAnswer = prompt(`Q${i + 1}: ${currentQuestion.question}`);
if (parseInt(userAnswer) === currentQuestion.answer) {
score++;
alert("✅ Correct!");
} else {
alert(`❌ Incorrect. Correct answer: ${currentQuestion.answer}`);
}
}
const endTime = Date.now();
const timeTaken = Math.floor((endTime - startTime) / 1000);
alert(
`🎉 Quiz complete!\nScore: ${score}/${totalQuestions}\nTime: ${timeTaken} seconds`
);
}
const startButton = document.getElementById("startGame");
const quizTypeSelect = document.getElementById("quizType");
const gameContainer = document.getElementById("gameContainer");
const resultContainer = document.getElementById("result");
let questionCount = 0;
let score = 0;
let startTime, endTime;
let currentAnswer = null;
function getQuestionByType(type) {
switch (type) {
case "square":
return generateSquareQuestion();
case "cube":
return generateCubeQuestion();
case "addition":
return generateAdditionQuestion();
case "subtraction":
return generateSubtractionQuestion();
case "multiplication":
return generateMultiplicationQuestion();
case "division":
return generateDivisionQuestion();
case "table":
return generateTableQuestion();
default: // random
const allTypes = [
"square",
"cube",
"addition",
"subtraction",
"multiplication",
"division",
"table",
];
const randomType = allTypes[Math.floor(Math.random() * allTypes.length)];
return getQuestionByType(randomType);
}
}
function showNextQuestion(type) {
if (questionCount >= 10) {
endTime = new Date();
const timeTaken = Math.floor((endTime - startTime) / 1000);
gameContainer.innerHTML = "";
resultContainer.innerHTML = `
✅ Quiz Complete!<br>
🧠 Score: ${score}/10<br>
⏱️ Time: ${timeTaken} seconds
`;
return;
}
const { question, answer } = getQuestionByType(type);
currentAnswer = answer;
gameContainer.innerHTML = `
<div>
<p><strong>Question ${questionCount + 1}:</strong> ${question}</p>
<input type="number" id="userInput" placeholder="Your answer" />
<button id="submitAnswer">Submit</button>
</div>
`;
document.getElementById("submitAnswer").addEventListener("click", () => {
const userInput = parseInt(document.getElementById("userInput").value);
if (userInput === currentAnswer) {
score++;
}
questionCount++;
showNextQuestion(type);
});
}
startButton.addEventListener("click", () => {
const selectedType = quizTypeSelect.value;
score = 0;
questionCount = 0;
resultContainer.innerHTML = "";
startTime = new Date();
showNextQuestion(selectedType);
});