-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (77 loc) · 3.02 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
let numeroSecreto = 0;
let intentos = 0;
let intentosMaximos = 0;
let listaNumerosSorteados = [];
let numeroMaximo = 10;
function asignarTextoElemento(elemento, texto) {
let elementoHTML = document.querySelector(elemento);
elementoHTML.innerHTML = texto;
}
function verificarIntento() {
let numeroDeUsuario = parseInt(document.getElementById('valorUsuario').value);
if (intentos >= intentosMaximos) {
asignarTextoElemento('p', '¡Has superado el número máximo de intentos!');
document.getElementById('reiniciar').removeAttribute('disabled');
return;
}
if (numeroDeUsuario === numeroSecreto) {
asignarTextoElemento('p', `¡Acertaste el número en ${intentos + 1} ${(intentos + 1 === 1) ? 'intento' : 'intentos'}!`);
document.getElementById('reiniciar').removeAttribute('disabled');
} else {
if (numeroDeUsuario > numeroSecreto) {
asignarTextoElemento('p', 'El número secreto es menor');
} else {
asignarTextoElemento('p', 'El número secreto es mayor');
}
intentos++;
if (intentos >= intentosMaximos) {
asignarTextoElemento('p', '¡Has superado el número máximo de intentos!');
document.getElementById('reiniciar').removeAttribute('disabled');
} else {
actualizarContadorIntentos();
}
limpiarCaja();
}
}
function limpiarCaja() {
document.querySelector('#valorUsuario').value = '';
}
function generarNumeroSecreto() {
let numeroGenerado = Math.floor(Math.random() * numeroMaximo) + 1;
if (listaNumerosSorteados.length == numeroMaximo) {
asignarTextoElemento('p', 'Ya se sortearon todos los números posibles');
} else {
if (listaNumerosSorteados.includes(numeroGenerado)) {
return generarNumeroSecreto();
} else {
listaNumerosSorteados.push(numeroGenerado);
return numeroGenerado;
}
}
}
function condicionesIniciales() {
asignarTextoElemento('h1', 'Juego del número secreto!');
asignarTextoElemento('p', `Indica un número del 1 al ${numeroMaximo}`);
asignarTextoElemento('#contadorIntentos', 'Esperando que ingreses el número de intentos...');
setTimeout(() => {
intentosMaximos = parseInt(prompt('¿Cuántos intentos deseas en total?'));
if (isNaN(intentosMaximos) || intentosMaximos <= 0) {
intentosMaximos = 10; // Valor predeterminado si la entrada no es válida
}
intentos = 0; // Inicializar intentos a 0
actualizarContadorIntentos();
numeroSecreto = generarNumeroSecreto();
}, 100);
}
function actualizarContadorIntentos() {
let intentosRestantes = intentosMaximos - intentos;
if (intentosRestantes >= 0) {
asignarTextoElemento('#contadorIntentos', `Intentos restantes: ${intentosRestantes}`);
}
}
function reiniciarJuego() {
limpiarCaja();
condicionesIniciales();
document.querySelector('#reiniciar').setAttribute('disabled', 'true');
}
window.onload = condicionesIniciales;