Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
From the code below as per 2nd line condition - if dice === 6 code must stop ! what's the need of reassigning dice variable at line 4 ? (I'm a begnr)
let dice = Math.trunc(Math.random() * 6) + 1;
while (dice !== 6) {
   console.log(`you rolled ${dice}`);
   dice = Math.trunc(Math.random() * 6) + 1;
   if (dice === 6) {
      console.log("Game over at 6");
   }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

At the first line dice is given a value between 1-6, if it is not 6 it goes inside the loop and tells you what you rolled. If we don't reassign the dice variable it results with dice being the same value at the first line and the code is stuck inside an infinite loop since the dice was not 6 and will never be 6. We need to reassign it so that the dice can become 6 if it was not at the first line and end the game.

PS. if the dice is 6 at the first line it wont print Game over.

about 4 years ago · Juan Pablo Isaza Relatório

0

On line 1 you give dice a random value between 1 and 6. On line 2 you state that while the value is not 6 you should do what's in the while loop. On line 4 you give dice a new random value between 1 and 6. Then you check if the code is 6.

Also I hope this code is very poorly written with bad understanding of programing. Don't expect to learn to code properly from the teacher that gave it to you.

Here is a better way to do it.

while true {
  let dice;
  dice = Math.trunc(Math.random() * 6) + 1;
  console.log(`you rolled ${dice}`);
  if (dice === 6) {
    console.log("Game over at 6");
    break; // this will end the while loop
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda