-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (71 loc) · 2.69 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Editar contenido del HTML con JS
//let parrafo = document.querySelector('p');
//parrafo.innerHTML = 'Indica un numero del 1 al 10';
let numeroSecreto = 0;
let intentos = 0;
let listaNumSecretos = [];
let numMax = 10;
//let numeroSecreto = generarNumSecreto();
//let intentos = 1;
// Creacion de una funcion en JS
function asignarTexElemento(elemento, texto) {
let elementoHTML = document.querySelector(elemento);
elementoHTML.innerHTML = texto;
return;
}
function verificarIntento() {
let numeroUsuario = parseInt(document.getElementById('valorUsuario').value);
//console.log(numeroUsuario === numeroSecreto);
if (numeroUsuario === numeroSecreto) {
asignarTexElemento('p', `Acertaste el numero Secreto en ${intentos} ${(intentos === 1) ? 'vez' : 'veces'}`);
document.getElementById('reiniciar').removeAttribute('disabled')
} else {
if (numeroUsuario > numeroSecreto) {
asignarTexElemento('p', 'El numero Secreto es Menor');
} else {
asignarTexElemento('p', 'El numero Secreto es Mayor');
}
intentos++;
limpiarCaja();
}
return;
}
function limpiarCaja() {
//let valorCaja = document.querySelector('#valorUsuario');
//valorCaja.value = '';
document.querySelector('#valorUsuario').value = '';
}
function generarNumSecreto() {
//let numeroSecreto = Math.floor(Math.random() * 10) + 1;
//return numeroSecreto;
//return Math.floor(Math.random() * 10) + 1;
let numGenerado = Math.floor(Math.random() * numMax) + 1;
console.log(numGenerado);
console.log(listaNumSecretos);
// Si ya sorteamos todos los numero
if (listaNumSecretos.length == numMax) {
asignarTexElemento('p', `Ya se sortearon todos los numeros posibles`)
} else {
// Si el numero esta generado en la lista
if (listaNumSecretos.includes(numGenerado)) {
// Recursividad
return generarNumSecreto();
} else {
listaNumSecretos.push(numGenerado);
return numGenerado;
}
}
}
function condicionInicio() {
asignarTexElemento('h1', 'Juego del numero escondido');
asignarTexElemento('p', `Indica un numero del 1 al ${numMax}`);
numeroSecreto = generarNumSecreto();
intentos = 1;
}
function reiniciarJuego() {
//Limpiar caja - Iniciar mensaje de intervalo de numeros - Generar numero aleatorio - desabilitar boton - reiniciar intentos
limpiarCaja();
condicionInicio();
document.querySelector('#reiniciar').setAttribute('disabled', 'true');
}
condicionInicio();