-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
123 lines (91 loc) · 3.44 KB
/
main.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
document.addEventListener("DOMContentLoaded",()=>{
createSquares();
let guessedWords = [[]]
let availableSpace = 1;
let word = "dairy"
let guessedWordCount=0;
const keys = document.querySelectorAll(".keyboard-row button")
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("")
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([])
}
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)
};
}
})