-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjavascript.js
152 lines (128 loc) · 4.17 KB
/
javascript.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
var playing = false;
var score;
var timeRemaining;
var action;
var correctAnswer;
//the user clicks on the start/reset
document.getElementById("start").onclick = function () {
//if the user is playing
if (playing == true) {
//reload page
location.reload();
}
//if the user is not playing
else {
//change mode to playing
document.getElementById("startgame").pause();
playing = true;
score = 0;
document.getElementById("scoreNumber").innerHTML = score;
//show the instructions
document.getElementById("instruction").innerHTML =
"Click on the right answer";
//show countdown box
show("time");
//30 seconds timer
timeRemaining = 30;
document.getElementById("remainingTime").innerHTML = timeRemaining;
//hide game over box
hide("gameover");
//change button to reset
document.getElementById("start").innerHTML = "Reset Game";
//start countdown
startCountdown();
//generate new question and answers
generateQA();
}
};
//the user clicks on the answer box
for (var i = 1; i < 5; i++) {
document.getElementById("answer" + i).onclick = function () {
if (playing == true) {
//if the answer is correct
//this=document.getElementById("answer1")
if (this.innerHTML == correctAnswer) {
//correct answer
score++;
document.getElementById("scoreNumber").innerHTML = score;
//play sound
document.getElementById("win").play();
show("right");
//show for 1 sec
setTimeout(function () {
hide("right");
}, 1000);
hide("wrong");
//generate new answer and question
generateQA();
} else {
//play sound
document.getElementById("lost").play();
//wrong answer
show("wrong");
//show for 1 sec
setTimeout(function () {
hide("wrong");
}, 1000);
hide("right");
}
}
};
}
//functions
//start countdown 20sec
const startCountdown = () => {
action = setInterval(function () {
timeRemaining -= 1;
document.getElementById("remainingTime").innerHTML = timeRemaining;
if (timeRemaining == 0) {
//game over
stopCountdown();
show("gameover");
document.getElementById("gameover").innerHTML =
"<p>GAME OVER!</p><p>YOUR SCORE: " + score + "</p>";
document.getElementById("final").play();
hide("time");
hide("right");
hide("wrong");
playing = false;
document.getElementById("start").innerHTML = "Start Game";
}
}, 1000);
};
//generate question and answers
const generateQA = () => {
//a random digit from 0 to 10 inclusive
var randomNumber1 = Math.round(Math.random() * 10);
var randomNumber2 = Math.round(Math.random() * 10);
document.getElementById("problem").innerHTML =
randomNumber1 + " x " + randomNumber2;
correctAnswer = randomNumber1 * randomNumber2;
var answerBox = Math.round(Math.random() * 3) + 1;
//to fill on if the random answer boxes with the right answer
document.getElementById("answer" + answerBox).innerHTML = correctAnswer;
//storing answer choices;
var answers = [correctAnswer];
//to fill the other answer boxes with the wrong answers
//make sure to exclude the box with the right answer
for (var i = 1; i < 5; i++) {
if (i !== answerBox) {
var wrongAnswer;
// check that the wrong answer is not equal to the right answer or another taken wrong answer
//do: at least one possible answer, while: generate then a new possible answer, if the previous answer is not ok
do {
wrongAnswer =
Math.round(Math.random() * 10) * Math.round(Math.random() * 10);
} while (answers.indexOf(wrongAnswer) > -1); //wrongAnswer is already in the answer list, we countinue do loop
document.getElementById("answer" + i).innerHTML = wrongAnswer;
//adding wrong answer to answer choices
answers.push(wrongAnswer);
}
}
};
//stop counter
const stopCountdown = () => clearInterval(action);
//hide an element
const hide = (id) => (document.getElementById(id).style.display = "none");
//show an element
const show = (id) => (document.getElementById(id).style.display = "block");