-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (79 loc) · 2.69 KB
/
script.js
File metadata and controls
93 lines (79 loc) · 2.69 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
/**
* @param {string} sel
* @return {Element[]} results
*/
const $$ = sel => Array.from(document.querySelectorAll(sel));
/**
* @param {string} tag
* @param {{[name: string]: string}} attrs
* @param {Array<Element|string>|Element|string} children
* @return {Element}
*/
const h = (tag, attrs, children=[]) => {
const tagData = tag.match(/(^|[#.])[^#.]*/g) || [];
const el = document.createElement(tagData.shift() || 'div');
tagData.forEach(str =>
str[0] === '.' ? el.classList.add(str.substr(1)) :
str[0] === '#' ? el.setAttribute('id', str.substr(1)) : '');
Object.keys(attrs).forEach(k => el.setAttribute(k, attrs[k]));
const kids = children instanceof Array ? children : [ children ];
kids.forEach(kid => (typeof kid === 'string')
? el.appendChild(document.createTextNode(kid))
: el.appendChild(kid));
return el;
};
const dictionary = fetch('./words-large.txt')
.then(resp => resp.text())
.then(txt => new Set(txt.toLowerCase().split('\n')));
/**
*
* @param {Element} el
*/
const removeColumn = (el) => {
const column = el.closest('.column');
if (!column) { return null; }
column.remove();
};
const addColumn = () => {
const domColumns = document.getElementById('columns');
if (!domColumns) { return null; }
domColumns.appendChild(h('li.column', {}, [
h('input.inColumn', { type: 'text', tabIndex: '1' }),
h('button.btnRemove', {tabIndex: '10'}, 'Remove')
]));
};
/**
*
* @param {Set<string>} words
* @param {string[][]} columns
* @param {string} prefix
* @param {string[]} results
* @return {string[]} the results
*/
const solve = (words, columns, prefix = '', results = []) => {
if (columns.length === 0 && words.has(prefix)) {
results.push(prefix);
} else if (columns.length > 0) {
const newColumns = columns.slice(1);
for(const char of columns[0]) {
solve(words, newColumns, prefix + char, results);
}
}
return results;
};
document.addEventListener('click', async ev => {
const target = /** @type {Element} */(ev.target);
const domAdd = target.closest('.btnAdd');
const domRemove = target.closest('.btnRemove');
const domSolve = target.closest('.btnSolve');
const domResult = document.querySelector('.result');
if (domAdd) { addColumn(); }
if (domRemove) { removeColumn(domRemove); }
if (domSolve && domResult) {
const domCols = /** @type {HTMLInputElement[]} */($$('.inColumn'));
const cols = domCols.map(col => col.value.toLowerCase().split(''));
const words = await dictionary;
const results = solve(words, cols);
domResult.innerHTML = results.join(', ');
}
});