/
wordle.js
208 lines (162 loc) · 5.56 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const letters = document.querySelectorAll(".flex-item")
const ANSWER_LENGTH = 5
const loadingIndicator = document.querySelector('.loading');
console.log(loadingIndicator.innerText)
const ROUNDS = 6
async function init() {
let currentGuess = ''
let currentRow = 0
let done = false
let isLoading = true;
setLoading(true);
// Let's go and grab the word of the Day!!
const res = await fetch("https://words.dev-apis.com/word-of-the-day")
const resObj = await res.json()
const word = resObj.word.toUpperCase();
setLoading(false)
isLoading = false;
console.log(word)
const wordParts = word.split("")
console.log(wordParts)
// We are making map of "right answer"
const map = makeMap(wordParts);
function isLetter(letter) {
return /^[a-zA-Z]$/.test(letter);
}
function addLetter(letter) {
// add the letter to the end !!
if (currentGuess.length < ANSWER_LENGTH) {
currentGuess += letter;
}
// replace the last letter!!
else {
currentGuess = currentGuess.substring(0, currentGuess.length - 1) + letter;
}
// This line will show the letters on the Screen!!
// "ANSWER_LENGHT * currentRow" in the below expression will make sure that each time
// we press enter, we get to write on the next row!!
letters[ANSWER_LENGTH * currentRow + currentGuess.length - 1].innerText = letter;
}
async function commit() {
if (currentGuess.length !== ANSWER_LENGTH) {
// do nothing!!
return;
}
// TODO : validate the word using API
// setting the loading state to true
isLoading = true
setLoading(true)
const res = await fetch("https://words.dev-apis.com/validate-word", {
method: "POST",
body: JSON.stringify({ word: currentGuess })
})
const resObj = await res.json()
const validWord = resObj.validWord;
// setting the loading state back to false
isLoading = false
setLoading(false)
if (!validWord) {
markInvalidWord();
return;
}
//The split() method splits a string into an array of substrings
const guessParts = currentGuess.split("");
for (let i = 0; i < ANSWER_LENGTH; i++) {
// Mark as Correct!!
if (wordParts[i] === guessParts[i]) {
letters[ANSWER_LENGTH * currentRow + i].classList.add('correct')
map[guessParts[i]]--;
}
}
for (let i = 0; i < ANSWER_LENGTH; i++) {
if (wordParts[i] === guessParts[i]) {
// do nothing
}
else if (wordParts.includes(guessParts[i]) && map[guessParts[i]] > 0) {
letters[ANSWER_LENGTH * currentRow + i].classList.add('close')
map[guessParts[i]]--;
}
else {
letters[ANSWER_LENGTH * currentRow + i].classList.add('wrong')
}
}
// TODO : win or loose situation !!
currentRow++;
if (currentGuess === word) {
// You win
// alert("Congrats!! You Won")
const title = document.querySelector(".title")
title.classList.add('winner')
done = true
return
}
else if (currentRow === ROUNDS) {
// you lost
alert(`You lost! Correct Word was ${word}`)
done = true
return
}
currentGuess = ''
}
function backspace() {
currentGuess = currentGuess.substring(0, currentGuess.length - 1);
letters[ANSWER_LENGTH * currentRow + currentGuess.length].innerText = '';
}
function markInvalidWord() {
// alert("Invalid Word!!");
for (let i = 0; i < ANSWER_LENGTH; i++) {
letters[ANSWER_LENGTH * currentRow + i].classList.remove('invalid')
setTimeout(function () {
letters[ANSWER_LENGTH * currentRow + i].classList.add('invalid')
}, 10)
}
}
document.addEventListener('keydown', function handleKeyPress(event) {
if (done || isLoading) {
// don nothing
return;
}
const action = event.key;
// console.log(action)
if (action === 'Enter') {
commit(); // which will save the word to compare it to the result!!
}
else if (action === 'Backspace') {
backspace();
}
else if (isLetter(action)) {
addLetter(action.toUpperCase());
}
else {
// Do Nothing !!
}
})
function setLoading(isLoading) {
// if(isLoading)
// {
// // page is being loaded up
// // so we need to show up the "indicator"
// loadingIndicator.classList.remove('hidden');
// }
// else{
// loadingIndicator.classList.add('hidden');
// }
//The toggleAttribute() method of the Element interface toggles a Boolean attribute
//(removing it if it is present and adding it if it is not present) on the given element.
loadingIndicator.classList.toggle('show', isLoading)
}
function makeMap(array) {
const obj = {}
for (let i = 0; i < array.length; i++) {
const letter = array[i]
if (obj[length]) {
obj[letter]++;
}
else {
obj[letter] = 1;
}
}
return obj
}
}
init();