-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrabbler.html
86 lines (77 loc) · 2.89 KB
/
scrabbler.html
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
<html>
<head><title>Scrabbler</title>
<script type="text/javascript" src="enable1.js">
</script>
<script type="text/javascript">
console.log(enable1.length);
var typewatch = function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}
}();
function update() {
var chars = document.getElementById('chars').value.toLowerCase();
console.log('new chars: ' + chars);
var important = chars.indexOf(' ') !== -1 ? chars.substring(0, chars.indexOf(' ')) : chars;
var regex = new RegExp("^[" + chars + "]+$");
var results = [];
for (var i in enable1) {
if (enable1.hasOwnProperty(i)) {
var word = enable1[i];
if (regex.test(word)) {
var xp = chars;
var importantChars = important;
var ok = true;
for (var c in word) if (word.hasOwnProperty(c)) {
var ch = word[c];
var index = xp.indexOf(ch);
if (index === -1) {
ok = false;
break;
}
// Set to space or remove? I dunno what is faster
xp = xp.replace(ch, ' ');
if (importantChars.indexOf(ch) !== -1) {
importantChars = importantChars.replace(ch, '');
}
}
if (ok) {
results.push({ text: word, score: importantChars.length });
}
}
}
}
results.sort(function (a, b) { return (a.score === b.score ? b.text.length - a.text.length : a.score - b.score); });
var resultHtml = '';
for (var r in results) {
console.log(results[r].text + " " + results[r].score);
resultHtml += results[r].text + " (" + results[r].score + '/' + results[r].text.length + ')<br/>';
}
document.getElementById('resultDiv').innerHTML = resultHtml;
}
</script>
<style>
body {
background-color: #000;
color: #bdf;
}
input {
width: 100%;
font-size:6vw;
font-family: sans-serif;
background-color: #000;
color: #fff;
}
div {
font-size:4vw;
font-family: sans-serif;
}
</style>
</head>
<body>
<input type="text" id="chars" oninput="typewatch(update, 500)" style="width:100%" />
<div id="resultDiv"></div>
</body>
</html>