-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
36 lines (30 loc) · 991 Bytes
/
ui.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
document
.getElementById("expression")
.addEventListener("keyup", e => evaluateExpression(e.target.value));
function evaluateExpression(expression) {
const boolio = new Boolio(expression);
renderTruthTable(boolio.truthTable(), document.getElementById("truth-table"));
}
function renderTruthTable(truthTable, htmlTable) {
const thead = htmlTable.querySelector("thead");
const tbody = htmlTable.querySelector("tbody");
thead.innerHTML = "";
tbody.innerHTML = "";
truthTable.atoms.forEach(atom => thead.appendChild(createTh(atom)));
thead.appendChild(createTh("="));
truthTable.rows.forEach(row => {
const tr = document.createElement("tr");
row.forEach(col => {
const td = document.createElement("td");
td.innerText = col ? "T" : "F";
tr.appendChild(td);
});
tbody.appendChild(tr);
});
}
function createTh(text) {
const th = document.createElement("th");
th.setAttribute("scope", "col");
th.innerText = text;
return th;
}