-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (44 loc) · 1.28 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
//Variáveis
const screenOne = document.querySelector('.screen-one');
const screenTwo = document.querySelector('.screen-two');
const btnTry = document.querySelector('#btnTry');
const btnPlayAgain = document.querySelector('#btnPlayAgain');
const randomNumber = Math.round(Math.random() * 10);
const pressEnter = document;
let xAttempts = 1;
//Eventos
btnTry.addEventListener('click', handleTryClick);
btnPlayAgain.addEventListener('click', handlePlayAgain);
pressEnter.addEventListener('keydown', handlePressEnter);
//funções
function handleTryClick(e) {
e.preventDefault(); //Previnir o padrão
const inputNumber = document.querySelector('#inputNumber');
if (inputNumber.value == '') {
xAttempts;
} else {
if (Number(inputNumber.value) == randomNumber) {
toggleScreen();
screenTwo.querySelector(
'h2'
).innerText = `Acertou em ${xAttempts} tentativas`;
}
inputNumber.value = '';
xAttempts++;
}
}
function handlePlayAgain() {
toggleScreen();
document.location.reload(true);
xAttempts = 1;
}
function toggleScreen() {
screenOne.classList.toggle('hide');
screenTwo.classList.toggle('hide');
}
function handlePressEnter(e) {
if (e.key == 'Enter' && screenOne.classList.contains('hide')) {
console.log(e);
handlePlayAgain();
}
}