-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (55 loc) · 1.44 KB
/
Copy pathscript.js
File metadata and controls
63 lines (55 loc) · 1.44 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
const numP = document.querySelector("#number");
const stateS = document.querySelector("#state");
const input = document.querySelector("#input");
const resultP = document.querySelector("#result");
const pointsP = document.querySelector("#points");
const submit = document.querySelector("#button");
const newNumB = document.querySelector("#new");
const binaryBox = document.querySelector("#binary");
const largeBox = document.querySelector("#large");
let points = 0;
let max = 16;
let num = Math.floor(Math.random() * max);
numP.innerHTML = num.toString(2);
submit.addEventListener('click', e => {
if (binaryBox.checked) {
if (input.value == num) {
success();
} else {
fail(num);
}
} else {
if (input.value == num.toString(2)) {
success();
} else {
fail(num.toString(2));
}
}
});
newNumB.addEventListener('click', e => {
if (largeBox.checked) {
max = 256;
} else {
max = 16;
}
num = Math.floor(Math.random() * max);
if (binaryBox.checked) {
numP.innerHTML = num.toString(2);
stateS.innerHTML = "decimal";
} else {
numP.innerHTML = num;
stateS.innerHTML = "binary";
}
});
function success() {
resultP.innerHTML = "Correct!";
points++;
if (points < 2) {
pointsP.innerHTML = points + " point";
} else {
pointsP.innerHTML = points + " points";
}
}
function fail(answer) {
resultP.innerHTML = "Sorry, the answer was <br/><span class='large'>" + answer + "</span>";
}