-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (66 loc) · 2.46 KB
/
app.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
let numeroSecreto = 0;
let intentos = 0;
let listaDeNumerosSorteados = [];
let numeroMaximo = 10;
//console.log(numeroSecreto);
//Funcion para seleccionar un elemento y asignarle un texto
function asignarTextoElemento (elemento, texto){
let elementoHTML = document.querySelector(elemento);
elementoHTML.innerHTML = texto;
return;
}
function verificarIntento() {
let numeroDeUsuario = parseInt(document.getElementById('valorUsuario').value);
//EL usuario adivino o no el numero secreto
if(numeroDeUsuario === numeroSecreto){
asignarTextoElemento('p',`Excelente adivinaste el numero secreto en ${intentos} ${(intentos === 1) ? 'intento' : 'intentos'}`);
document.getElementById('reiniciar').removeAttribute('disabled');
} else {
if (numeroDeUsuario > numeroSecreto){
asignarTextoElemento('p','El numero secreto es menor');
} else{
asignarTextoElemento('p','El numero secreto es mayor');
}
intentos++;
limpiarCampo();
}
return;
}
function limpiarCampo(){
document.querySelector('#valorUsuario').value = '';
}
function generarNumeroSecreto() {
//Generar un numero aleatorio entre 1 y 10
let numeroGenerado = Math.floor(Math.random()*numeroMaximo) + 1;
console.log(numeroGenerado);
console.log(listaDeNumerosSorteados);
//si ya sorteamos todos los numeros posibles
if( listaDeNumerosSorteados.length == numeroMaximo){
asignarTextoElemento('p','Ya se han sorteado todos los numero posibles');
}else {
//si el nuemero generado ya esta en la lista de numeros sorteados
if (listaDeNumerosSorteados.includes(numeroGenerado)){
return generarNumeroSecreto();
} else {
listaDeNumerosSorteados.push(numeroGenerado);
return numeroGenerado;
}
}
}
function condicionesIniciales(){
asignarTextoElemento('h1', 'Juegos del numero secreto JB');
asignarTextoElemento('p', `Adivina el numero secreto entre 1 y ${numeroMaximo}`);
numeroSecreto = generarNumeroSecreto();
intentos = 1;
}
function reniciaJuego(){
//Limpiar campo
limpiarCampo();
//indicar mensaje de inicio de numero
//generar el numero secreto
//inicializar intentos
condicionesIniciales();
//deshabilitar boton reiniciar
document.querySelector('#reiniciar').setAttribute('disabled', true);
}
condicionesIniciales();