-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-api.js
134 lines (102 loc) · 4.29 KB
/
script-api.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
133
134
(async function () {
const req = await fetch('https://random-word-api.herokuapp.com/word?length=5')
const res = await req.json()
const secretWord = res[0]
const letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
const previousAttempts = []
const maxAttempts = 5
const wordsLength = secretWord.length
const wordsContainer = document.getElementById("wordsContainer")
for (let i = 0;i < maxAttempts * wordsLength;i++) {
const letterBox = document.createElement('span')
const letterBoxContainer = document.createElement('span')
letterBox.classList.add("border", "size-16", "text-4xl", "font-bold", "grid", "place-items-center", "rounded-lg")
letterBoxContainer.classList.add("rounded-lg")
letterBoxContainer.appendChild(letterBox)
wordsContainer.appendChild(letterBoxContainer)
}
let attemptCount = 0
let letterCount = 0
window.addEventListener("keydown", handleKeys)
async function handleKeys(e) {
if (e.key == 'Enter' && letterCount == wordsLength) {
let word = ''
for (let i = (attemptCount * wordsLength);i < (attemptCount * wordsLength) + wordsLength;i++) {
word += wordsContainer.children[i].textContent
}
word = word.toLowerCase()
if (!(await wordExists(word))) {
setShakeError()
return
}
if (word != secretWord) {
if (previousAttempts.includes(word)) {
alert("You've already tried this bro 🙂")
removeEventListener("keydown", handleKeys)
return
}
previousAttempts.push(word)
setShakeError()
showCorrectLetters(word)
letterCount = 0
attemptCount++
if (attemptCount == maxAttempts) {
alert(`The word was ${secretWord} 😒`)
removeEventListener("keydown", handleKeys)
location.reload()
return
}
return
}
showCorrectLetters(word)
setTimeout(() => alert('You did it 🥳\nRefresh the page to get new wordle!'), 200)
}
if (e.key == 'Backspace' && letterCount > 0) {
const letterBox = wordsContainer.children[(attemptCount * wordsLength) + (--letterCount)].children[0]
letterBox.textContent = ''
return jumpZoom(letterBox)
}
if (letterCount == wordsLength) {
return
}
if (!letters.includes(e.key)) {
return
}
const box = wordsContainer.children[(attemptCount * wordsLength) + letterCount].children[0]
box.textContent = e.key.toUpperCase()
jumpZoom(box)
letterCount++
}
function setShakeError() {
for (let i = (attemptCount * wordsLength);i < (attemptCount * wordsLength) + wordsLength;i++) {
let el = wordsContainer.children[i]
el.classList.remove("error-shake")
void el.offsetWidth
el.classList.add("error-shake")
}
}
function jumpZoom(el) {
el.classList.remove("jump-zoom")
void el.offsetWidth
el.classList.add("jump-zoom")
}
function showCorrectLetters(word) {
let j = 0
for (let i = (attemptCount * wordsLength);i < (attemptCount * wordsLength) + wordsLength;i++) {
let el = wordsContainer.children[i]
if (secretWord[j] == word[j]) {
el.classList.add("corret-letter-correct-position")
} else if (secretWord.includes(word[j])) {
el.classList.add("corret-letter-wrong-position")
} else {
el.classList.add("wrong-letter-wrong-position")
}
j++
}
}
async function wordExists(word) {
const req = await fetch("https://api.dictionaryapi.dev/api/v2/entries/en/" + word)
const res = await req.json()
return !!res[0].word
}
})()