-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (132 loc) · 4.46 KB
/
script.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function next(id) {
if (document.getElementById(id-1).value.length == 0) {
if (document.getElementById(id-1)) {
document.getElementById(id-1).focus()
}
} else {
if (document.getElementById(id)) {
document.getElementById(id).focus()
}
}
if (id <= 5) {
check(5)
} else if (id > 5 && id <= 10) {
check(10)
}else if (id > 10 && id <= 15) {
check(15)
}else if (id > 15 && id <= 20) {
check(20)
}else if (id > 20 && id <= 25) {
check(25)
}else if (id > 25 && id <= 30) {
check(30)
}
}
document.addEventListener("keydown", function(event) {
id = parseFloat(document.getElementById(document.activeElement.getAttribute('id')).id)
if (event.key == "Backspace") {
document.getElementById(id).value = ''
document.getElementById(id-1).focus()
event.preventDefault()
} else if (event.key == 'ArrowLeft') {
document.getElementById(id-1).focus()
} else if (event.key == 'ArrowRight') {
document.getElementById(id+1).focus()
} else {
if (document.getElementById(id).value != '') {
document.getElementById(id+1).focus()
}
}
})
function check(id) {
id = id-5
input = document.getElementById(id).value
input += document.getElementById(id+1).value
input += document.getElementById(id+2).value
input += document.getElementById(id+3).value
input += document.getElementById(id+4).value
if (input.length == 5) {
if (words.includes(input.toLowerCase()) || possibilities.includes(input.toLowerCase())) {
letter(id, 0)
letter(id+1, 1)
letter(id+2, 2)
letter(id+3, 3)
letter(id+4, 4)
if (document.getElementById(id+5)) {
document.getElementById(id+5).focus()
}
if (input == word) {
document.getElementById('gg').style.display = 'block'
document.getElementById('definition').innerHTML = 'definition: ' + definition
} else {
if (id == 25) {
document.getElementById('lost').style.display = 'block'
document.getElementById('word').innerHTML = 'word: ' + word
document.getElementById('definition').innerHTML = 'definition: ' + definition
}
}
} else {
shake(id)
shake(id+1)
shake(id+2)
shake(id+3)
shake(id+4)
}
}
}
function letter(id, pos) {
if (document.getElementById(id).value == word[pos]) {
document.getElementById(id).classList.add('c')
document.getElementById(document.getElementById(id).value).classList.add('y')
document.getElementById(id).disabled = true
} else if (word.includes(document.getElementById(id).value)) {
document.getElementById(id).classList.add('p')
document.getElementById(document.getElementById(id).value).classList.add('y')
document.getElementById(id).disabled = true
} else {
document.getElementById(id).classList.add('i')
document.getElementById(document.getElementById(id).value).classList.add('n')
document.getElementById(id).disabled = true
}
if (document.getElementById(id+5)) {
document.getElementById(id+5).disabled = false
}
}
function clear() {
inputs = document.getElementsByTagName("input")
for (i = 0; i < inputs.length; i++) {
if (inputs[i].type != "submit" && inputs[i].type != "reset") {
inputs[i].value = ""
if (i > 5) {
inputs[i].disabled = true
}
}
}
}
function shake(id) {
document.getElementById(id).classList.add('shake')
setTimeout(() => {
document.getElementById(id).classList.remove('shake')
}, 300);
}
clear()
document.getElementById('0').focus()
async function start() {
found = false
while (!found) {
word = possibilities[Math.floor(Math.random() * possibilities.length)]
await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
.then(response => response.json())
.then(data => {
try {
definition = data[0].meanings[0].definitions[0].definition;
if (definition.length > 0) {
found = true
}
} catch (error) {}
})
console.log(word)
word.split('')
}
}
start()