mirror of
https://github.com/CristianCruz0309/juego_secreto.git
synced 2026-08-15 02:50:08 +00:00
Ajuste del valor máximo
This commit is contained in:
@@ -0,0 +1,75 @@
|
|||||||
|
let numeroSecreto = 0;
|
||||||
|
let Intentos = 0;
|
||||||
|
let numerosSorteados = [];
|
||||||
|
let numeroMaximo = 100;
|
||||||
|
|
||||||
|
function asignarTextoElemento(elemento, texto) {
|
||||||
|
let elementoHTML = document.querySelector(elemento);
|
||||||
|
elementoHTML.innerHTML = texto;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function VerificarIntento() {
|
||||||
|
let numeroDeUsuario = parseInt(document.getElementById('valorUsuario').value);
|
||||||
|
|
||||||
|
console.log(Intentos);
|
||||||
|
if (numeroDeUsuario === numeroSecreto) {
|
||||||
|
asignarTextoElemento('p',`Acertaste el número en ${Intentos} ${(Intentos === 1) ? 'vez!' : 'veces!'}`);
|
||||||
|
document.getElementById('reiniciar').removeAttribute('disabled');
|
||||||
|
} else {
|
||||||
|
// El usuario no acertó
|
||||||
|
if (numeroDeUsuario > numeroSecreto) {
|
||||||
|
asignarTextoElemento('p','El número secreto es menor');
|
||||||
|
} else {
|
||||||
|
asignarTextoElemento('p','El número secreto es mayor')
|
||||||
|
}
|
||||||
|
Intentos++;
|
||||||
|
limpiarCaja();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function limpiarCaja() {
|
||||||
|
document.querySelector('#valorUsuario').value = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
function generarNumeroSecreto() {
|
||||||
|
let numeroGenerado = Math.floor(Math.random()*numeroMaximo)+1;
|
||||||
|
|
||||||
|
console.log(numeroGenerado);
|
||||||
|
console.log(numerosSorteados);
|
||||||
|
// Si ya sorteamos todos los números
|
||||||
|
if (numerosSorteados.length == numeroMaximo) {
|
||||||
|
asignarTextoElemento('p', 'Ya se sortearon todos los números posibles!')
|
||||||
|
} else {
|
||||||
|
// Si el número generado está incluido en la lista
|
||||||
|
if (numerosSorteados.includes(numeroGenerado)) {
|
||||||
|
return generarNumeroSecreto();
|
||||||
|
} else {
|
||||||
|
numerosSorteados.push(numeroGenerado);
|
||||||
|
return numeroGenerado;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function condicionesIniciales() {
|
||||||
|
asignarTextoElemento('h1', 'Juego del número secreto!');
|
||||||
|
asignarTextoElemento('p', `Indica un número del 1 al ${numeroMaximo}!`);
|
||||||
|
numeroSecreto = generarNumeroSecreto();
|
||||||
|
Intentos = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function reiniciarJuego() {
|
||||||
|
// Limpiar caja
|
||||||
|
limpiarCaja();
|
||||||
|
// Indicar mensaje de intervalo de números
|
||||||
|
// Generar el número aleatorio
|
||||||
|
// Inicializar el número de intentos
|
||||||
|
condicionesIniciales();
|
||||||
|
// Deshabilitar el botón de nuevo juego
|
||||||
|
document.querySelector('#reiniciar').setAttribute('disabled','true');
|
||||||
|
}
|
||||||
|
|
||||||
|
condicionesIniciales();
|
||||||
+1
-1
@@ -21,7 +21,7 @@
|
|||||||
<h1></h1>
|
<h1></h1>
|
||||||
<p class="texto__parrafo"></p>
|
<p class="texto__parrafo"></p>
|
||||||
</div>
|
</div>
|
||||||
<input type="number" min="1" max="10" class="container__input">
|
<input type="number" min="1" max="100" class="container__input">
|
||||||
<div class="chute container__botones">
|
<div class="chute container__botones">
|
||||||
<button class="container__boton">Intentar</button>
|
<button class="container__boton">Intentar</button>
|
||||||
<button class="container__boton" id="reiniciar" disabled>Nuevo juego</button>
|
<button class="container__boton" id="reiniciar" disabled>Nuevo juego</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user