-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (59 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
const bdd = {
c: "a;b",
e: "b;d",
f: "c;e",
g: "c;e;f",
h: "f;e",
k: "g;h;c",
l: "c;e;h",
m: "h",
n: "m;e;k",
o: "c;a;n",
p: "c;d;e;f;g;h;i;j;k;l;m;n;o"
};
$('#graph').click(_=>{
$("#result").toggle()
})
var button = document.getElementById("searchInferenceButton");
button.addEventListener("click", searchResult, false);
let allLettersWeNeed;
function searchResult() {
allLettersWeNeed = new Set()
const search = document.getElementById("searchInference").value;
if (!bdd.hasOwnProperty(search)) return $('#error').text(`${search} doesn't exist in the bdd. Try one of these: ${Object.keys(bdd)}`)
else $('#error').empty()
let resultString = ``;
const chain = searchOccurenceInBdd(search);
const jq = format(chain)
$("#result")
.empty()
.append(jq)
$("#allLettersWeNeed")
.empty()
.append(Array.from(allLettersWeNeed))
}
function format(chain) {
if (chain.tempChain && chain.tempChain.length) {
letters = chain.tempChain.map(partial=>format(partial))
allLettersWeNeed.add(chain.letter)
return $('<div>').append(
chain.letter,
$('<br/>'),
$('<span>').append(letters)
)
} else {
allLettersWeNeed.add(chain);
return $('<span>').text(chain)
}
}
function searchOccurenceInBdd(letter) {
if (bdd.hasOwnProperty(letter)) {
const tempChain = bdd[letter]
.split(";")
.map(letterToSearch => searchOccurenceInBdd(letterToSearch));
return {
letter,
tempChain
};
} else return letter;
}