-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
195 lines (146 loc) · 3.74 KB
/
game.js
File metadata and controls
195 lines (146 loc) · 3.74 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
let question = document.getElementById('question');
let answer_box = document.getElementById('answer');
let score = document.getElementById('score');
let incorrect = document.getElementById('incorrect');
answer_box.addEventListener('keypress', input_listener);
let selected_chapter = parseInt(sessionStorage.getItem("chapter"));
let digit_slider = document.getElementById('digits');
let size_slider = document.getElementById('size');
let size = 5;
let digits = 1;
digit_slider.oninput = function() {
digits = this.value;
load_question();
}
size_slider.oninput = function() {
size = this.value;
load_question();
}
load_question();
function load_question() {
eval(`load_question_${selected_chapter}()`);
}
function go_main_page() {
window.location.href="index.html";
}
function get_random(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function input_listener(e) {
if (e.keyCode === 13) {
check_answer();
}
return false;
}
function check_answer() {
if(!answer) {
return;
}
if(answer === parseInt(answer_box.value)) {
answer_box.value = "";
score.innerHTML = parseInt(score.innerHTML) + 1;
}
else {
answer_box.value = "";
incorrect.innerHTML = parseInt(incorrect.innerHTML) + 1;
return;
}
load_question();
}
function load_question_1() {
digit_slider.remove();
size_slider.remove();
let a = get_random(101, 9999);
let b = get_random(101, 999);
question.innerHTML = a + " + " + b;
answer = a + b;
}
function load_question_2() {
digit_slider.remove();
size_slider.remove();
let a = get_random(1000, 9999);
let b = get_random(101, 999);
question.innerHTML = a + " - " + b;
answer = a - b;
}
function load_question_3() {
digit_slider.remove();
size_slider.remove();
let a = get_random(101, 999);
let b = get_random(2, 9);
question.innerHTML = a + " x " + b;
answer = a * b;
}
function load_question_4() {
digit_slider.remove();
size_slider.remove();
let a = get_random(11,99);
question.innerHTML = a + "²";
answer = a * a;
}
function load_question_5() {
digit_slider.remove();
size_slider.remove();
let a = get_random(11, 99);
let b = get_random(11, 99);
question.innerHTML = a + " x " + b;
answer = a * b;
}
function load_question_6() {
digit_slider.remove();
size_slider.remove();
let a = get_random(101, 999);
question.innerHTML = a + "²";
answer = a * a;
}
function load_question_7() {
digit_slider.remove();
size_slider.remove();
let a = get_random(11, 99);
question.innerHTML = a + "³";
answer = a * a * a;
}
//
// d|n
// 1|1 - 9
// 2|10 - 99
// 3|100 - 999
// 4|1000 - 9999
//
function load_question_8() {
let lower = Math.pow(10, digits - 1);
let upper = Math.pow(10, digits) - 1;
sums = [];
for(let i = 0; i < size; i++) {
sums.push(get_random(lower, upper));
}
let string = "";
sums.forEach(num => {
string += num.toString() + "+";
});
question.innerHTML = string.substring(0, string.length - 1);
answer = sums.reduce((a, b) => a + b);
}
//free real estate
function load_question_9() {
let a = get_random(11, 99);
question.innerHTML = a + "³";
answer = a * a * a;
}
function load_question_10() {
let a = get_random(1000, 9999);
let b = get_random(101, 999);
question.innerHTML = a + " - " + b;
answer = a - b;
}
function load_question_11() {
let a = get_random(101, 999);
let b = get_random(2, 9);
question.innerHTML = a + " x " + b;
answer = a * b;
}
function load_question_12() {
let a = get_random(11,99);
question.innerHTML = a + "²";
answer = a * a;
}