-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
180 lines (132 loc) · 5.28 KB
/
app.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
App = async ()=>{
const tileContainer = document.querySelector(".tile-container")
const popupContainer = document.querySelector(".popup-container")
const keyboard = document.querySelector(".key-container")
let word = "TRACK";
let response = await fetch("https://random-word-api.herokuapp.com/word?number=5&length=10")
response = await response.json()
console.log(response)
wordlist = response
word = response[Math.floor(Math.random()*10)].toUpperCase()
let activeGame = true
const keys = "Q.W.E.R.T.Y.U.I.O.P.A.S.D.F.G.H.J.K.L.ENTER.Z.X.C.V.B.N.M.BACK".split(".")
const tileRows = [
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""],
["", "", "", "", ""]
]
const congratulations = ["Genius!", "Amazing!", "Impressive!", "Nicely Done!", "You got it!", "Phew!"]
let currentRow = 0;
let currentTile = 0;
let currentGuess = "";
document.addEventListener("keydown", (event)=>{
keyClick(event.key)
})
keyClick = (key_lowercase) => {
if (!activeGame)
return;
let key = key_lowercase.toUpperCase()
if (key.length == 1){
if (currentTile < 10){
const tile = document.getElementById('guessRow-'+currentRow+"-tile-"+currentTile)
tile.innerText = key;
currentGuess += key;
currentTile++;
}
}
else if (key === "BACKSPACE" || key === "BACK"){
if (currentTile > 0){
const tile = document.getElementById('guessRow-'+currentRow+"-tile-"+(currentTile-1))
tile.innerText = "";
currentGuess = currentGuess.slice(0, -1);
currentTile--;
}
}
else if (key === "ENTER"){
if (currentGuess.length < 10){
showPopUp("Guess must be 10 letters long")
return;
}
//let guess = document.getElementById('guessRow-'+currentRow)
guessMade()
}
}
guessMade = async () =>{
//later: check if the word entered is a real word
let response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${currentGuess}`)
response = await response.json()
const isWordFake = (response.result_msg == `No Definitions Found`)
if (isWordFake){
showPopUp("Not a real word")
return false;
}
const checked = new Map([])
for (let index = 0 ; index < 10 ; index++){
const tile = document.getElementById('guessRow-'+currentRow+"-tile-"+index)
const curKey = document.getElementById(tile.innerText)
setTimeout(()=>{
tile.classList.add("flip")
if(!checked.has(tile.innerText) && word[index]==tile.innerText){
tile.classList.add("green-tile")
curKey.classList.add("green-tile")
checked.set(tile.innerText, true)
}
if(!checked.has(tile.innerText) && word.includes(tile.innerText)){
tile.classList.add("yellow-tile")
curKey.classList.add("yellow-tile")
checked.set(tile.innerText, true)
}
tile.classList.add("grey-tile")
curKey.classList.add("grey-tile")
}, 500*index)
}
//check if the guess is correct
if (currentGuess === word){
activeGame = false;
showPopUp(congratulations[currentRow])
} //later: remove event listeners
//reset guessed word, tile index and increment row index
currentGuess = ""
currentTile = activeGame ? 0 : 5
currentRow = currentRow+1
if (currentRow == 10){
if (currentGuess !== word){
activeGame = false;
}
}
if (!activeGame)
showPopUp(`The word is ${word}`)
}
showPopUp = (message) => {
const popup = document.createElement('div')
popup.classList.add("popup")
popup.innerText = message
popupContainer.append(popup)
setTimeout(()=>{popupContainer.removeChild(popup)},2000)
}
tileRows.forEach((tileRow, rowIndex) => {
const tileRowElement = document.createElement('div')
tileRowElement.setAttribute('id', 'guessRow-'+rowIndex)
tileRowElement.classList.add("tile-row")
tileRow.forEach((tile, tileIndex) => {
const tileElement = document.createElement('div')
tileElement.setAttribute('id', 'guessRow-'+rowIndex+"-tile-"+tileIndex)
tileElement.classList.add('tile')
tileRowElement.append(tileElement)
})
tileContainer.append(tileRowElement)
})
keys.forEach(key => {
const keybtn = document.createElement('button')
keybtn.innerText = key
keybtn.setAttribute('id', key)
keybtn.className = "key"
keybtn.addEventListener("click", ()=>{keyClick(key)})
keyboard.append(keybtn)
// += `<button class="key" id=${key}>${key}</button>`
})
}
App()