-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (56 loc) · 1.49 KB
/
index.js
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
function onBodyLoad() {
const footer = document.querySelector("#user-agent");
footer.textContent = window.navigator.userAgent;
const input = document.querySelector("#input");
input.addEventListener("input", onInput);
input.addEventListener(
"keydown",
(event) => {
if (event.ctrlKey && event.code === "Enter") {
run();
}
}
);
}
function onInput() {
const input = document.querySelector("#input");
const button = document.querySelector("#button");
if (input.value.length > 0) {
button.removeAttribute("disabled");
} else {
button.setAttribute("disabled", "");
}
}
function run() {
const input = document.querySelector("#input");
const xEvaluatedTo = document.querySelector("#x-evaluated-to");
const output = document.querySelector("#output");
const toEval = `x = ${input.value}`;
console.debug("toEval", toEval);
let x = undefined;
let result;
try {
eval(toEval);
xEvaluatedTo.textContent = `const x = ${format(x)};`;
const { isInfinite, examples } = giveExamples(x);
if (examples.length > 0) {
if (isInfinite) {
result = examples
.map((example, index) => `x == ${format(example, index)}`)
.join("\n")
.concat("\n…");
} else {
result = examples
.map(example => `x == ${format(example)}`)
.join("\n");
}
} else {
result = `Nothing is loosely equal to ${format(x)}.`;
}
} catch (error) {
console.error(error);
xEvaluatedTo.textContent = "undefined";
result = error.stack;
}
output.textContent = result;
}