-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (40 loc) · 1.36 KB
/
Copy pathscript.js
File metadata and controls
52 lines (40 loc) · 1.36 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
let message = document.getElementById("message");
let score = 0; // Player's score
let num1, num2, correctAnswer, operation;
// Generate a new math question
function generateQuestion() {
num1 = Math.floor(Math.random() * 10) + 1; // Random number between 1-10
num2 = Math.floor(Math.random() * 10) + 1;
const operations = ["+", "-", "*"];
operation = operations[Math.floor(Math.random() * operations.length)]; // Randresult
// Calculate the correct answer
switch (operation) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
}
// Display the question
document.getElementById("question").textContent = `Solve: ${num1} ${operation} ${num2}`;
}
// Check player's answer
document.getElementById("submit").addEventListener("click", () => {
const userinp = parseFloat(document.getElementById("answer").value);
if (userinp === result) {
score++;
message.innerHTML="WOW! You Are Genius🎉";
}
else {
message.innerHTML=(`Wrong! The correct answer was ${result}.`);
}
document.getElementById("score").textContent = `Score: ${score}`;
document.getElementById("answer").value = ""; // Clear the input
generateQuestion(); // Generate a new question
});
// Start the game with the first question
generateQuestion();