-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
304 lines (274 loc) · 10.6 KB
/
Copy pathindex.html
File metadata and controls
304 lines (274 loc) · 10.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Add to homescreen for Chrome on Android -->
<meta name="mobile-web-app-capable" content="yes" />
<link rel="icon" sizes="192x192" href="images/android-desktop.png" />
<!-- Add to homescreen for Safari on iOS -->
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta
name="apple-mobile-web-app-title"
content="Material Design Lite"
/>
<link
rel="apple-touch-icon-precomposed"
href="images/ios-desktop.png"
/>
<title>Math Problems</title>
<meta name="description" content="HTML template" />
<meta name="author" content="Your name here" />
<link
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
<link
rel="stylesheet"
href="https://code.getmdl.io/1.3.0/material.teal-lime.min.css"
/>
<script
defer
src="https://code.getmdl.io/1.3.0/material.min.js"
></script>
<script src="https://unpkg.com/mithril/mithril.js"></script>
<style>
.my-isactive {
color: #ffffff;
}
.my-answer-input {
font-size: 56px;
text-align: center;
}
.my-header .mdl-layout__header-row {
padding-left: 40px;
}
.my-container {
max-width: 1600px;
width: calc(100% - 16px);
margin: 0 auto;
}
.my-content {
border-radius: 2px;
padding: 80px 56px;
margin-bottom: 80px;
text-align: center;
}
.my-layout.is-small-screen .my-content {
padding: 40px 28px;
}
.my-content h3 {
margin-top: 48px;
}
</style>
</head>
<body>
<!-- content goes here -->
</body>
</html>
<script>
let root = document.body;
let numberRight = 0;
let numberTotal = 0;
let num_limit = 10;
let operands = [0, 0];
let opOptions = [
{ id: '+', text: 'Add' },
{ id: '-', text: 'Subtract' },
{ id: 'x', text: 'Multiply' },
];
let currentOp = opOptions[0];
let buttonText = 'Check';
let userInput = '';
const rangeButtonHandler = (e) => {
if (num_limit >= 1000) num_limit = 10;
else num_limit *= 10;
// get new values given new range
getNewValues();
};
const operationButtonHandler = (e) => {
let newOp = findOptionByValue(e.target.textContent);
if (newOp) {
currentOp = newOp;
if (currentOp.id == '-') operands.sort();
} else {
console.log('Operation not found! (' + e.target.textContent);
}
opButtons = document.querySelectorAll('.opbutton');
opButtons.forEach((b) => b.classList.remove('my-isactive'));
e.target.classList.add('my-isactive');
getNewValues();
};
const getNewValues = () => {
operands.forEach((n, i) => {
return (operands[i] = Math.floor(Math.random() * num_limit));
});
// ascending order for subtraction so we don't have negative results
if (currentOp.id == '-') operands.sort();
};
const inputInputHandler = (e) => {
userInput = e.target.value;
};
const findOptionById = (e) => {
return opOptions.find(({ id }) => id == e.target.value);
};
const findOptionByValue = (val) => {
return opOptions.find(({ text }) => text == val);
};
const findOptionIndex = (e) => {
let i = 0;
return opOptions.find(({ id }) => {
i++;
if (id == e.target.value) return i;
else return -1;
});
};
const answerButtonHandler = (e) => {
if (buttonText == 'Correct - click to continue') {
userInput = '';
buttonText = 'Check';
getNewValues();
return;
}
switch (currentOp.id) {
case '+':
answer = operands[1] + operands[0];
break;
case '-':
answer = operands[1] - operands[0];
break;
case 'x':
answer = operands[1] * operands[0];
break;
}
if (answer == Number(userInput)) {
numberRight++;
buttonText = 'Correct - click to continue';
} else {
numberTotal++;
buttonText = 'Try again';
}
};
var Question = {
oninit: getNewValues,
view: function () {
return m('div.mdl-layout.mdl-js-layout.mdl-layout--fixed-header', [
m(
'header.mdl-layout__header',
m('div.mdl-layout__header-row', [
m('span.mdl-layout-title', 'Math Practice'),
m('div.mdl-layout-spacer'),
m('nav.mdl-navigation.mdl-layout--large-screen-only', [
m(
'button.mdl-button mdl-js-button rangebutton',
{ onclick: rangeButtonHandler },
'From 0 to ',
num_limit,
),
//m("a.mdl-navigation__link[href='']", 'Link'),
opOptions.map((o) => {
let classString =
'button.mdl-button mdl-js-button opbutton';
if (o == currentOp) {
classString += ' my-isactive';
}
return m(
classString,
{ onclick: operationButtonHandler },
o.text,
);
}),
]),
]),
),
m('div.mdl-layout__drawer', [
m('span.mdl-layout-title', 'Math Practice'),
m('nav.mdl-navigation', [
m(
'button.mdl-button mdl-js-button rangebutton',
{ onclick: rangeButtonHandler },
'From 0 to ',
num_limit,
),
//m("a.mdl-navigation__link[href='']", 'Link'),
opOptions.map((o) => {
let classString =
'button.mdl-button mdl-js-button opbutton';
if (o == currentOp) {
classString += ' my-isactive';
}
return m(
classString,
{ onclick: operationButtonHandler },
o.text,
);
}),
]),
]),
m('main.mdl-layout__content', [
//<div class='demo-container mdl-grid'>
m('div.my-container mdl-grid', [
m(
'div.mdl-cell mdl-cell--2-col mdl-cell--hide-tablet mdl-cell--hide-phone',
),
//<div class="demo-content mdl-color--white mdl-shadow--4dp content mdl-color-text--grey-800 mdl-cell mdl-cell--8-col"></div>
m(
'div.my-content mdl-color--white mdl-shadow--4dp content mdl-color-text--grey-800 mdl-cell mdl-cell--8-col',
[
m('h3', 'Enter your answer below:'),
m(
'h1',
operands[1] +
' ' +
currentOp.id +
' ' +
operands[0],
),
m('div.mdl-textfield mdl-js-textfield', [
m(
'input.mdl-textfield__input my-answer-input[type=number]',
{
oninput: inputInputHandler,
value: userInput,
},
),
m(
'label.mdl-textfield__label[style="text-align: center"]',
{ for: 'answer' },
'Answer',
),
]),
m('div', [
m(
'button.mdl-button mdl-js-button mdl-button--primary',
{
onclick: answerButtonHandler,
},
buttonText,
),
]),
m('p', numberRight + '/' + numberTotal),
],
),
]),
]),
m('footer.mdl-mini-footer', [
m('.mdl-mini-footer__left-section', [
m(
'a[href="https://github.com/mdenson/math-practice"].mdl-logo',
'Math Practice',
),
]),
m('.mdl-mini-footer__right-section', [
m('ul.mdl-mini-footer__link-list', [
m('li', 'Brock Denson'),
m('li', 'MIT License'),
]),
]),
]),
]);
},
};
m.mount(root, Question);
</script>