-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
70 lines (60 loc) · 1.52 KB
/
script.js
File metadata and controls
70 lines (60 loc) · 1.52 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
// form validation
const form = document.getElementById('subscribe');
const fname = document.getElementById('fname');
const lname = document.getElementById('lname');
const email = document.getElementById('lname');
form.onsubmit = function(e) {
if(!fname.value) {
e.preventDefault();
}
if(!lname.value) {
e.preventDefault();
}
if(!fname.value) {
e.preventDefault();
}
}
// math logic
let question = '';
let answer = 0;
const generate = document.getElementById('generate');
const check = document.getElementById('check');
const problem = document.getElementById('problem');
const userAnswer = document.getElementById('answer');
generate.addEventListener('click', function() {
doMath(question, answer);
problem.innerText = question;
userAnswer.value = '';
});
check.addEventListener('click', function() {
if (userAnswer.value == answer) {
alert('Correct');
}
else{
alert('Incorrect\nTry again');
}
});
function doMath() {
// const randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
const x = Math.floor(Math.random() * 100 ) + 1;
const y = Math.floor(Math.random() * 100 ) + 1;
const operator = Math.floor(Math.random() * 4) + 1;
switch (operator) {
case 1:
question = `${x} + ${y}`;
answer = x + y;
break;
case 2:
question = `${x} - ${y}`;
answer = x - y;
break;
case 3:
question = `${x} x ${y}`;
answer = x * y;
break;
case 4:
question = `${x} / ${y}`;
answer = (x/y).toFixed(2);
break;
}
}