-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
123 lines (112 loc) · 3.6 KB
/
main.js
File metadata and controls
123 lines (112 loc) · 3.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
let config = {
non: 4,
lower_limit: 0,
upper_limit: 10,
target: 24,
showAnswer: false
}
function populate_config() {
document.getElementById("non").innerHTML = config.non;
document.getElementsByName("lower-limit")[0].value = config.lower_limit;
document.getElementsByName("upper-limit")[0].value = config.upper_limit;
document.getElementsByName("target")[0].value = config.target;
document.getElementsByName("showAnswer")[0].checked = config.showAnswer;
setAutoTab();
}
function modify_config() {
config.lower_limit = parseFloat(document.getElementsByName("lower-limit")[0].value);
config.upper_limit = parseFloat(document.getElementsByName("upper-limit")[0].value);
config.target = parseFloat(document.getElementsByName("target")[0].value);
config.showAnswer = document.getElementsByName("showAnswer")[0].checked;
setAutoTab();
}
function increase_non() {
config.non += 1
document.getElementById("non").innerHTML = config.non;
var node = document.createElement("input");
node.classList.add("number");
node.value = 0
node.type = "number"
document.getElementsByClassName("numbers")[0].append(" ");
document.getElementsByClassName("numbers")[0].append(node);
}
function decrease_non() {
config.non -= 1
document.getElementById("non").innerHTML = config.non;
let numbers = document.getElementsByClassName("numbers")[0];
numbers.removeChild(numbers.lastElementChild);
}
function solve_wrap() {
var n_html = document.getElementsByClassName("number");
let numbers = [];
for (var i = 0; i < n_html.length; i++) {
numbers.push(parseFloat(n_html[i].value));
}
document.getElementById('answer').innerHTML = "calculating..."
setTimeout(function() {
const ans = solve(numbers, config.target);
if (ans && config.showAnswer) {
document.getElementById('answer').innerHTML = ans;
} else if (ans && !config.showAnswer) {
document.getElementById('answer').innerHTML = "<details><summary>solution exists (click to show)</summary><br>" + ans + "</details>";
} else {
document.getElementById('answer').innerHTML = "no solution";
}
}, 10);
}
function generate_new() {
var n_html = document.getElementsByClassName("number");
let solved = false
while (solved === false) {
var numbers = [];
for (var i = 0; i < n_html.length; i++) {
numbers.push(random_int(0, 10))
}
solved = solve(numbers)
}
for (var i = 0; i < n_html.length; i++) {
n_html[i].value = numbers[i]
}
// clear answer div
document.getElementById('answer').innerHTML = ""
}
function clear_numbers() {
var n_html = document.getElementsByClassName("number");
for (var i = 0; i < n_html.length; i++) {
n_html[i].value = ""
}
}
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
*/
function random_int(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function toggle_config() {
var x = document.getElementById("config");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function setAutoTab() {
var n_html = document.getElementsByClassName("number");
for (var i = 0; i < n_html.length - 1; i++) {
let current_element = n_html[i]
let next_element = n_html[i+1]
current_element.onkeyup = () => {
current_value = parseFloat(current_element.value);
if (current_value === 0 || current_value*10 > config.upper_limit) {
next_element.focus();
next_element.select();
}
}
}
}
(function() {
populate_config();
generate_new();
setAutoTab();
})();