Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Tiene problemas para aleatorizar la elección usando (Math.floor(Math.random() * 3)). Cuando se aplica a la variable "computerChoice", siempre resulta el mismo número

Cuando se hace clic en el botón de la página, siempre se obtiene el mismo resultado, que es 1 o "La riqueza te encontrará". He estado jugando y ajustando durante mucho tiempo y no he podido encontrar una solución. Cualquier consejo o sugerencia sería apreciado, ¡incluso solo un lugar para comenzar a solucionar problemas!

 let computerChoice = (Math.floor(Math.random() * 3)); function clickEvent() { if (computerChoice === 0) { alert("You will perish soon."); } else if (computerChoice === 1) { alert("Wealth will find you."); } else if (computerChoice === 2) { alert("Love is in the air."); } }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <script src="index.js"></script> <title>8Ball</title> </head> <body> <h1 class=header>Cower in Fear, the Magic 8 Ball is here</h1> <div class="container-one"> <img class="hero-image" src="Images/8ball.jpeg" alt="8ball"> </div> <div class="container-two"> <!--<button id="action">Swirl the 8ball</button>--> <input id="action" type="button" value="Swirl" onClick="clickEvent();" /> <h3 class="button-text">Hit the swirl button to discover what the future holds!</h3> </div> </body> </html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Esto debería funcionar como se esperaba. Coloqué la variable computerChoice dentro de la función clickEvent para que genere los números aleatorios como se esperaba.

 const swirl_btn = document.querySelector('#action') function clickEvent() { let computerChoice = (Math.floor(Math.random() * 3)); if (computerChoice === 0) { alert("You will perish soon."); } else if (computerChoice === 1) { alert("Wealth will find you."); } else if (computerChoice === 2) { alert("Love is in the air."); } } swirl_btn.addEventListener('click', function() { clickEvent() })
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="style.css"> <script src="index.js"></script> <title>8Ball</title> </head> <body> <h1 class=header>Cower in Fear, the Magic 8 Ball is here</h1> <div class="container-one"> <img class="hero-image" src="Images/8ball.jpeg" alt="8ball"> </div> <div class="container-two"> <!--<button id="action">Swirl the 8ball</button>--> <input id="action" type="button" value="Swirl" /> <h3 class="button-text">Hit the swirl button to discover what the future holds!</h3> </div> </body> </html>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda