-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
80 lines (65 loc) · 2.37 KB
/
index.html
File metadata and controls
80 lines (65 loc) · 2.37 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
<!DOCTYPE html>
<html>
<body>
<p>Enter your guess: a four letter word, with no repeated letters</p>
<input type="text" id="input" name="comment" autocomplete="off" value="" autofocus=true/>
<script>
fetch("https://raw.githubusercontent.com/Omicron18/masterword/master/words2.txt")
.then( response => response.text() )
.then( text => setAnswer(text) )
function setAnswer(text) {
words = text.trim().split("\n");
answer=words[Math.floor(Math.random()*words.length)];
console.log(answer);
const input = document.getElementById('input');
input.addEventListener('change', entered);
}
function entered() {
var word=document.getElementById('input').value;
feedback(word,answer);
if (word===answer) {
var s = document.createElement('p');
s.textContent="you win!";
document.body.appendChild(s);
document.body.removeChild(input);
} else {
input.value="";
}
}
function feedback(word,answer) {
var ans = answer.split('');
var wo = word.split('');
if (word.length!=4 || !words.includes(word)) {
var s = document.createElement('pre');
s.textContent="["+word+"] - invalid guess";
document.body.appendChild(s);
return;
}
var right="";
for (var i=0; i<4; i++) {
if (ans[i]==wo[i]) {
right+="+";
}
}
var rightLetter="";
var rightN=right.length;
for (var i=0; i<4; i++) {
if (ans.includes(wo[i])) {
rightN--;
if (rightN<0) {
rightLetter+="/";
}
}
}
var s = document.createElement('pre');
s.textContent=word+' ';
if (right.length==0 && rightLetter.length==0) {
s.textContent+='.';
} else {
s.textContent+=right+rightLetter;
}
document.body.appendChild(s);
}
</script>
</body>
</html>