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

191
Visualizações
How to avoid a condition from looping?

I have to code a console Bingo game for JavaScript. It is one of some exercices that I have to do before a Bootcamp that I will be taking part of, so take in mind that i'm a newbie. In case someone doesn't know the game:

  • You will have a "card" showing 15 numbers (not repeated and random)
  • Each turn a random number (bingo ball) will be shown.
  • The numbers are from 1 until 90, so either balls and bingo cards will have these numbers.
  • When a ball has the same number as one of the numbers from your card, the number of your card will be changed to an 'X'.

Now that I've explained it, my problem is the following: I have a function to generate generate a ball with a random number each turn. In order to know if a number was already out or not, I've created an array to push the numbers that were already out. This way we can make a loop with an if condition to check if the ball has the same value as the arrays[i] number. The way I've done it, starts well, but ends up messing the chrome's console... as close as it gets to have the 90 numbers in the array, it starts to iterate the array and generate random numbers until it finds the lasts numbers remaining.

I will paste the part of the code I'm talking about here below.

function bingo(){
   console.table(bingoCard);
   bombo();
   for (let i = 0; i < bingoCard.length; i++){
      if (bola === bingoCard[i].number){
         bingoCard[i].number = 'X';
         bingoCard[i].matched = true;
      }
   }
   continuar = confirm('¿Continuar?');

   if (continuar === true){
      console.table(bingoCard);
      bingo();
   }else {
      return 'Hasta la próxima';
   }
}

function randomNum(){
   let min = 1;
   let max = 90;
   return Math.floor(Math.random() * (max - min) + min);
}
         
function bombo(){

   bola = randomNum();
   console.log(+ bola + 'antes de bucle'); //test
   for (let i = 0; i < numbersOut.length; i++){
      if (bola === numbersOut[i]){
         bingo();
      }
   }
   numbersOut.push(bola);
   console.log(numbersOut);
   alert('Ha salido el número ' + bola);   
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can instead create an array with numbers from 1 to 90 and random shuffle it.

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
}

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Modern_method - if you want to understand this method

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