-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
52 lines (39 loc) · 1.16 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
// Variáveis
let randomNumber = Math.round(Math.random() * 10)
const screen1 = document.querySelector('.screen-1')
const screen2 = document.querySelector('.screen-2')
const btnTry = document.querySelector('#btnTry')
const btnReset = document.querySelector('#btnReset')
const inputNumber = document.querySelector('#inputNumber')
let xAttempts = 1
console.log(randomNumber)
// Eventos
btnTry.addEventListener('click', handleTryClick)
btnReset.addEventListener('click', handleResetClick)
// funções
function handleTryClick(event) {
event.preventDefault()
if (Number(inputNumber.value == randomNumber)) {
toggleScreen()
screen2.querySelector(
'h2'
).innerText = `Você acertou em ${xAttempts} tentativas.`
document.addEventListener('keypress', function (e) {
if (e.key == 'Enter' && screen1.classList.contains('hide')) {
handleResetClick()
}
})
}
inputNumber.value = ''
xAttempts++
}
function handleResetClick() {
toggleScreen()
xAttempts = 1
randomNumber = Math.round(Math.random() * 10)
console.log(randomNumber)
}
function toggleScreen() {
screen1.classList.toggle('hide')
screen2.classList.toggle('hide')
}