-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
91 lines (75 loc) · 2.38 KB
/
Copy pathapp.js
File metadata and controls
91 lines (75 loc) · 2.38 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
// Definição de variáveis
let listaNumerosSorteados = [];
let numeroSecreto = gerarNumeroAleatorio();
let tentativas = 1;
// Chamada de funções
exibirMensagemInicial();
// Definição de funções
function exibirTextoNaTela(tag, texto)
{
let campo = document.querySelector(tag);
campo.innerHTML = texto;
responsiveVoice.speak(texto, 'Brazilian Portuguese Female', {rate:1.2});
}
function verificarChute()
{
let chute = document.querySelector('input').value;
if(chute == numeroSecreto)
{
exibirTextoNaTela('h1', 'Parabéns!');
if (tentativas == 1)
{
exibirTextoNaTela('p', `Você descobriu o número secreto com ${tentativas} tentativa!`);
document.getElementById('reiniciar').removeAttribute('disabled');
} else {
exibirTextoNaTela('p', `Você descobriu o número secreto com ${tentativas} tentativas!`);
document.getElementById('reiniciar').removeAttribute('disabled');
}
} else {
if (chute > numeroSecreto)
{
exibirTextoNaTela('h1', 'Errou!');
exibirTextoNaTela('p', `O número secreto é menor que ${chute}!`);
} else {
exibirTextoNaTela('h1', 'Errou!');
exibirTextoNaTela('p', `O número secreto é maior que ${chute}!`);
}
tentativas++;
limparCampo();
}
}
function gerarNumeroAleatorio()
{
let numeroEscolhido = parseInt(Math.random() * 100 + 1);
let quantidadeElementosLista = listaNumerosSorteados.length;
if(quantidadeElementosLista == 100)
{
listaNumerosSorteados = [];
}
if (listaNumerosSorteados.includes(numeroEscolhido))
{
return gerarNumeroAleatorio();
} else {
listaNumerosSorteados.push(numeroEscolhido);
console.log(listaNumerosSorteados)
return numeroEscolhido;
}
}
function limparCampo()
{
chute = document.querySelector('input');
chute.value = '';
}
function reiniciarJogo()
{
numeroSecreto = gerarNumeroAleatorio();
document.getElementById('reiniciar').setAttribute('disabled',true);
limparCampo();
tentativas = 1;
exibirMensagemInicial();
}
function exibirMensagemInicial()
{
exibirTextoNaTela('h1', 'Jogo do Número Secreto');
exibirTextoNaTela('p', 'Escolha um número entre 1 e 100');
}