-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
155 lines (134 loc) · 5.98 KB
/
script.js
File metadata and controls
155 lines (134 loc) · 5.98 KB
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
const wordOfTheDay = getWord(wordsList)
let wordOfTheDayTest = wordOfTheDay.map(obj => obj)
const allowesKeys = ["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", "enter", "backspace", "⇦"]
let screen = document.getElementById('screen')
let screenChildren = screen.children
//chamar essa funcao toda vez que o enter for apertado, desabilitando os slots com conteudo
const unableScreenSlots = () =>{
for (let child of screenChildren){
if(child.value != ''){
}
}
enteredKeys = []
}
const receiveKey = (event) =>{
if (event.key){
key = event.key.toLowerCase()
} else {
key = event.currentTarget.outerText.toLowerCase()
}
// insere a entrada apenas nos quadrados vazios, respeitando o limite de 5 caracteres
if(allowesKeys.includes(key)){
if (enteredKeys.length < 5 && key != 'backspace' && key != 'enter' && key != '⇦'){
for (let child of screenChildren){
if(child.value === '' && !child.getAttributeNames().includes('disabled')){
child.value = key.toUpperCase()
break
}
}
enteredKeys.push(key)
}
// chama a função de análise e tratamento após enter, caso o usuário tenha entrado com os 5 caracteres
if (key === 'enter' && enteredKeys.length === 5){
insertKeys(enteredKeys)
unableScreenSlots()
enteredKeys = []
wordOfTheDayTest = wordOfTheDay.map(obj => obj)
}
if (key === 'backspace' || key === '⇦'){ //
for (let child of screenChildren){
if(child.value != '' && child.nextElementSibling?.value === '' && !child.getAttributeNames().includes('disabled')){
child.value = ''
}
}
enteredKeys.pop()
}
}
}
let contScreenChild = 0
const insertKeys = (enteredKeys) => {
if(enteredKeys.toString() === wordOfTheDayTest.toString()){
for (let child of screenChildren){
child.value = ''
child.setAttribute('disabled', '')
child.classList.remove('button-almost')
child.classList.remove('button-error')
child.classList.add('button-correct')
}
let final = document.getElementById('final')
let finalMsg = document.createTextNode('Acertou, parabéns!!!')
final.appendChild(finalMsg)
setTimeout(() => {
for (let child of screenChildren){
child.value = '!'
}
final.classList.add('ativo')
}, 500);
} else {
let count = 0
let actualKey = ''
let countEachTyped = 0
//inicia pela marcaçã dos caracteres na posição correta
for(let eachTyped of enteredKeys){
actualKey = document.getElementsByClassName(`key${eachTyped.toUpperCase()}`)
if (eachTyped === wordOfTheDayTest[countEachTyped]){
screen.children[contScreenChild].classList.remove('button-inactive')
screen.children[contScreenChild].classList.add('button-correct')
screen.children[contScreenChild].setAttribute('disabled', '')
wordOfTheDayTest[countEachTyped] = '*'
actualKey[0].classList.add('key-correct')
}
contScreenChild++
countEachTyped++
}
contScreenChild = contScreenChild-5
for (let each of enteredKeys){
actualKey = document.getElementsByClassName(`key${each.toUpperCase()}`)
if (each === wordOfTheDayTest[count]){
screen.children[contScreenChild].classList.remove('button-inactive')
screen.children[contScreenChild].classList.add('button-correct')
screen.children[contScreenChild].setAttribute('disabled', '')
wordOfTheDayTest[count] = '*'
actualKey[0].classList.add('key-correct')
} else {
if (wordOfTheDayTest.includes(each)){
screen.children[contScreenChild].classList.add('button-almost')
screen.children[contScreenChild].setAttribute('disabled', '')
wordOfTheDayTest[wordOfTheDayTest.lastIndexOf(each)] = '*'
actualKey[0].classList.add('key-almost')
//se incluir mas nao bater o index, pinta de amarelo
}
else {
screen.children[contScreenChild].classList.remove('button-inactive')
screen.children[contScreenChild].classList.add('button-error')
screen.children[contScreenChild].setAttribute('disabled', '')
actualKey[0].classList.add('key-error')
//pinta a celular de cinza e vai para a linha de baixo
}
}
contScreenChild++
count++
}
wordOfTheDayTest = wordOfTheDay.map(obj => obj)
}
if (screenChildren[29].value != '' && screenChildren[29].value != undefined){
setTimeout(() => {
for (let child of screenChildren){
child.setAttribute('disabled', '')
child.classList.remove('button-almost')
child.classList.remove('button-error')
child.classList.remove('button-correct')
child.classList.add('inactive')
child.value = '!'
}
let final = document.getElementById('final')
let finalMsg = document.createTextNode('Errou feio, hein?! Tente novamente!')
final.classList.add('inactive')
final.appendChild(finalMsg)
}, 500);
}
}
document.addEventListener('keydown', (event) => {receiveKey(event)})
document.querySelectorAll(".key-item").forEach((el) => {
el.addEventListener('click', (event) => {receiveKey(event)})
})