-
Notifications
You must be signed in to change notification settings - Fork 2
/
Wordle.js
161 lines (128 loc) · 4.4 KB
/
Wordle.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
document.addEventListener("DOMContentLoaded", () => {
createSquares();
getNewWord();
let guessedWords = [[]];
let availableSpace = 1;
let word;
let guessedWordCount = 0;
const keys = document.querySelectorAll(".keyboard-row button");
function getNewWord() {
fetch(
`https://wordsapiv1.p.rapidapi.com/words/?random=true&lettersMin=5&lettersMax=5`,
{
method: "GET",
headers: {
"x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
"x-rapidapi-key": "<YOUR_KEY_GOES_HERE>",
},
}
)
.then((response) => {
return response.json();
})
.then((res) => {
word = res.word;
})
.catch((err) => {
console.error(err);
});
}
function getCurrentWordArr() {
const numberOfGuessedWords = guessedWords.length;
return guessedWords[numberOfGuessedWords - 1];
}
function updateGuessedWords(letter) {
const currentWordArr = getCurrentWordArr();
if (currentWordArr && currentWordArr.length < 5) {
currentWordArr.push(letter);
const availableSpaceEl = document.getElementById(String(availableSpace));
availableSpace = availableSpace + 1;
availableSpaceEl.textContent = letter;
}
}
function getTileColor(letter, index) {
const isCorrectLetter = word.includes(letter);
if (!isCorrectLetter) {
return "rgb(58, 58, 60)";
}
const letterInThatPosition = word.charAt(index);
const isCorrectPosition = letter === letterInThatPosition;
if (isCorrectPosition) {
return "rgb(83, 141, 78)";
}
return "rgb(181, 159, 59)";
}
function handleSubmitWord() {
const currentWordArr = getCurrentWordArr();
if (currentWordArr.length !== 5) {
window.alert("Word must be 5 letters");
}
const currentWord = currentWordArr.join("");
fetch(`https://wordsapiv1.p.rapidapi.com/words/${currentWord}`, {
method: "GET",
headers: {
"x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
"x-rapidapi-key": "61c5e3986dmsh20c1bee95c2230dp18d1efjsn4668bbcfc1b3",
},
})
.then((res) => {
if (!res.ok) {
throw Error();
}
const firstLetterId = guessedWordCount * 5 + 1;
const interval = 200;
currentWordArr.forEach((letter, index) => {
setTimeout(() => {
const tileColor = getTileColor(letter, index);
const letterId = firstLetterId + index;
const letterEl = document.getElementById(letterId);
letterEl.classList.add("animate__flipInX");
letterEl.style = `background-color:${tileColor};border-color:${tileColor}`;
}, interval * index);
});
guessedWordCount += 1;
if (currentWord === word) {
window.alert("Congratulations!");
}
if (guessedWords.length === 6) {
window.alert(`Sorry, you have no more guesses! The word is ${word}.`);
}
guessedWords.push([]);
})
.catch(() => {
window.alert("Word is not recognised!");
});
}
function createSquares() {
const gameBoard = document.getElementById("board");
for (let index = 0; index < 30; index++) {
let square = document.createElement("div");
square.classList.add("square");
square.classList.add("animate__animated");
square.setAttribute("id", index + 1);
gameBoard.appendChild(square);
}
}
function handleDeleteLetter() {
const currentWordArr = getCurrentWordArr();
const removedLetter = currentWordArr.pop();
guessedWords[guessedWords.length - 1] = currentWordArr;
const lastLetterEl = document.getElementById(String(availableSpace - 1));
lastLetterEl.textContent = "";
availableSpace = availableSpace - 1;
}
for (let i = 0; i < keys.length; i++) {
keys[i].onclick = ({ target }) => {
const letter = target.getAttribute("data-key");
if (letter === "enter") {
handleSubmitWord();
return;
}
if (letter === "del") {
handleDeleteLetter();
return;
}
updateGuessedWords(letter);
};
}
});