const insertNum = ()=>{
return new Promise((resolve, reject) => {
const numUsuario = Number(window.prompt("Introduce un número (1 - 6):"));
// Pide al usuario que introduzca un número
const aleatorio = Math.floor(Math.random() * 6 + 1);
// Elige un número aleatorio del 1 al 6
if (isNaN(numUsuario)) {
reject(new Error("Tipo de entrada incorrecta"));
// Si el usuario introduce un valor que no es un número,
// ejecuta reject con un error
}
if (numUsuario === aleatorio) {
// Si el número del usuario coincide con el número aleatorio,
// devuelve 2 puntos
resolve({
puntos: 2,
aleatorio,
});
} else if (numUsuario === aleatorio - 1 || numUsuario === aleatorio + 1) {
// Si el número del usuario es diferente al número aleatorio por 1,
// devuelve 1 punto
resolve({
puntos: 1,
aleatorio,
});
} else {
// Si no, devuelve 0 puntos
resolve({
puntos: 0,
aleatorio,
});
}
});
const continuarJuego = () => {
return new Promise((resolve) => {
if (window.confirm("¿Quieres continuar?")) {
// Pregunta si el usuario quiere continuar el juego
// con un modal de confirmación
resolve(true);
} else {
resolve(false);
}
});
};
}
const suponer = async () => {
try {
const result = await insertNum();
// En lugar del método 'then', podemos obtener el resultado
// directamente, poniendo 'await' antes de la promesa
alert(`Dado: ${result.aleatorio}: obtuviste ${result.puntos} puntos`);
const estaContinuando = await continuarJuego();
if (estaContinuando) {
suponer();
} else {
alert("Terminó el juego");
}
} catch (error) {
// En lugar del método 'catch', podemos usar la sintaxis 'try/catch'
alert(error);
};
}
insertNum()
suponer()
In this code, when executing it with the respective html, the error appears indicating that the function executeGame() is not defined, although I see it defined. In addition, the alert that asks for the number appears twice. If someone can help me to correct this situation, I will be very grateful. I changed the order of some functions, considering that it is asynchronous javascript. The code is neat and quite readable. Now, the runGame function only has relve, I don't know if it can work with just that parameter or if it also needs to include reject.
As a contribution to your answer, in reality I think you must have been looking for a long time and surely you cannot grasp what is happening to you. I'll help you with a breather.
There are 2 things that had to be done minimal, but vital and they are the following:
insertNum
2 times. At the end of your code independently, that is to say that you only call it but that call dies there and the second call is the one that you must actually execute and that is when you call the functionsuponer
, since it makes the call to the functioninsertNum
continuarJuego
certainly exists, but not where it should. Since you have created it in the scope of the functionsuponer
more specific in the promise of that function. And where you need it is at the same level as the functionsuponer
andinsertNum
.Considering this, I share the working code with the 2 fixes mentioned above: