-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (51 loc) · 1.5 KB
/
index.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
const form = document.getElementById('form');
form.addEventListener('submit', handleSubmit);
let status = document.getElementById('status');
let attempt = document.getElementById('attempt');
let result = document.getElementById('result');
const Guess = {
max: 10,
attemptsNumber: 0,
numberDrawn: function randomValue() {
return Math.round(Math.random() * this.max);
}
};
let numberDrawn = Guess.numberDrawn();
function updateAttempt(attempt, value) {
attempt.innerHTML = 'Tentativa: ' + value
};
function handleSubmit(e) {
e.preventDefault();
let kick = document.getElementById('kick').value;
if(!kick) {
alert('Digite algum valor!')
return;
};
updateAttempt(attempt, ++Guess.attemptsNumber);
if(numberDrawn == kick) {
playAgain();
status.innerHTML = 'Parabéns, você acertou!';
result.style.transition = '0.4s';
result.style.backgroundColor = '#37c978';
result.style.color = '#fff';
status.style.color = '#fff';
clear();
} else if(numberDrawn > kick) {
status.innerHTML = 'O número é maior!';
status.style.color = '#de4251';
clear();
} else if(numberDrawn < kick) {
status.innerHTML = 'O número é menor!';
status.style.color = '#de4251';
clear();
}
};
function playAgain() {
document.getElementById('btnRestart').style.display = 'flex';
};
function restart() {
document.location.reload(true);
};
function clear() {
document.getElementById('kick').value = '';
};