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

223
Vistas
While loop multiple statements in js. "The Dice Game"

I have to create a dice roller game, if it lands on 2,3,6,12 the game is supposed to stop and tell me which of the numbers the sum of both my dices landed on and how many "rolls" or tries it took me to get there. I haven't been able to figure out why my program isn't working, if I erase the conditions in the while loop and leave only one for example: while(dice != 2); the program will run but it would obviously only match if it lands on 2. Please help me!

  function play() {
    var dice = -1;
    var rolls = 1;
    var numb1 = 2;
    var numb2 = 3;
    var numb3 = 6;
    var numb4 = 12;
    while (dice != numb1 || dice != numb2 || dice != numb3 || dice != numb4) {
      dice = Math.floor(Math.random() * 11) + 2;
      rolls++;

    }

    document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
    document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;
    
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Change your || to && in your while

function play() {
  var dice = -1;
  var rolls = 1;
  var numb1 = 2;
  var numb2 = 3;
  var numb3 = 6;
  var numb4 = 12;
  while (dice != numb1 && dice != numb2 && dice != numb3 && dice != numb4) {
    dice = Math.floor(Math.random() * 11) + 2;
    rolls++;

  }

  document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
  document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;

}

play()
<div id="results"></div>
<div id="rolls"></div>

you can improve the while condition by using includes

function play() {
  var dice = -1;
  var rolls = 1;
  var numb1 = 2;
  var numb2 = 3;
  var numb3 = 6;
  var numb4 = 12;
  while (![numb1, numb2, numb3, numb4].includes(dice)) {
    dice = Math.floor(Math.random() * 11) + 2;
    rolls++;

  }

  document.getElementById('results').innerHTML = 'Your dice landed on: ' + dice;
  document.getElementById('rolls').innerHTML = 'Rolls: ' + rolls;

}

play()
<div id="results"></div>
<div id="rolls"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your code doesn't work, because dice would need to be each of these numbers at the same time to work, which is impossible.

Try doing this:

while (![2, 3, 6, 12].includes(dice)) {

Also, your random function can't get 1, maybe try:

Math.floor(Math.random() * 12) + 1
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