-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
87 lines (70 loc) · 2.34 KB
/
game.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
let score = 0;
let scoreBox = document.getElementById("score");
let questionBox = document.getElementById("question");
let answerForm = document.getElementById("quizForm");
function setAdditionGame() {
answerForm.setAttribute("data-gametype", "addition");
additionQuiz();
}
function setSubtractionGame() {
answerForm.setAttribute("data-gametype", "subtraction");
subtractionQuiz();
}
function setMultiplicationGame() {
answerForm.setAttribute("data-gametype", "multiplication");
multiplicationQuiz();
}
function setDivisionGame() {
answerForm.setAttribute("data-gametype", "division");
divisionQuiz();
}
function checkAnswer(){
let gametype= answerForm.getAttribute("data-gametype");
if (answerForm["answer"].value == answerForm["rightAnswer"].value) {
alert("Hey! WELL DONE!");
score ++;
} else {
alert("Oh NO!!! :(");
score --;
}
answerForm["answer"].value ="";
scoreBox.textContent = score;
if (gametype == "addition"){
additionQuiz();
} else if (gametype == "subtraction") {
subtractionQuiz();
} else if (gametype == "multiplication") {
multiplicationQuiz();
} else if (gametype == "division") {
divisionQuiz();
}
return false;
}
function additionQuiz(){
let num1 = Math.floor(Math.random() * 50);
let num2 = Math.floor(Math.random() * 50);
questionBox.textContent = num1+ " + " + num2+ " ?";
answerForm["rightAnswer"].value = (num1 + num2);
}
additionQuiz();
function subtractionQuiz(){
let num1 = Math.floor(Math.random() * 50);
let num2 = Math.floor(Math.random() * num1);
questionBox.textContent = num1+ " - " + num2 + " ?";
answerForm["rightAnswer"].value = (num1 - num2);
}
subtractionQuiz();
function multiplicationQuiz(){
let num1 = Math.floor(Math.random() * 50);
let num2 = Math.floor(Math.random() * 50);
questionBox.textContent = num1+ " * " + num2+ " ?";
answerForm["rightAnswer"].value = (num1 * num2);
}
multiplicationQuiz();
function divisionQuiz(){
let num2 = Math.round((Math.random() * 50)+1);
let num1 = Math.round((Math.random() * 50) + num2);
questionBox.textContent = num1+ " / " + num2 + " ? (Round your answer to the nearest whole number).";
answerForm["rightAnswer"].value = Math.round(num1 / num2);
}
divisionQuiz();