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

304
Visualizações
¿Por qué es este bucle infinito?

Estoy tratando de colocar tres cadenas en un tamaño de matriz de 9 que las contiene tres cadenas colocadas en índices aleatorios en la matriz, pero la forma en que intento hacerlo resulta en un ciclo infinito

 const ticketArray = []; const victoryMultipliers = ["x10", "x20", "x30", "x40", "x50", "x60"]; const threeOfThem = []; let arr = [] let randStr = ""; for (let i = 0; i < 3; i++) { threeOfThem[i] = victoryMultipliers[Math.floor(Math.random() * victoryMultipliers.length)] randStr = threeOfThem[i] //console.log(randStr) arr = threeOfThem.filter(val => val === randStr) if (arr.length >= 2) { threeOfThem.pop(); i--; } } const threeTicketArray = threeOfThem; /// So above we have an array of example ["x20", "x50", "x30"] // so below is where we have some problem let randInt; let tempStr = ""; for (let i = 0; i < 10; i++) { // i want to make an array.length of 9 randInt = Math.floor( Math.random() * 3 ); tempStr = threeTicketArray[randInt]; // to find the "x30" or whichever i stored ticketArray.push(tempStr); // push it into the ticketArray let len = ticketArray.filter(val => val === tempStr) // we filter to bring out the once for this loops iteration if ( len.length > 3 ) { // if I have 4 of example "x30" ticketArray.pop(); // I remove it as it was the last "x30" string added to the array i--; // I subtract the iteration by 1 so I can restart the this iteration of the loop } } // the end of the loop console.log(ticketArray);

Entonces, al azar, quiero que ticketArray tenga un tamaño de 9 , las cadenas de boletos se colocarán en un índice aleatorio y cada cadena aparecerá 3 veces. Sin embargo, estoy obteniendo un bucle infinito. ¡No puedo entender por qué!

about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

Vea la solución a continuación y dígame lo que piensa.

 const ticketArray = []; const victoryMultipliers = ["x10", "x20", "x30", "x40", "x50", "x60"]; const threeOfThem = []; let arr = [] let randStr = ""; for (let i = 0; i < 3; i++) { threeOfThem[i] = victoryMultipliers[Math.floor(Math.random() * victoryMultipliers.length)] randStr = threeOfThem[i] //console.log(randStr) arr = threeOfThem.filter(val => val === randStr) if (arr.length >= 2) { threeOfThem.pop(); i--; } } const threeTicketArray = threeOfThem; /// So above we have an array of example ["x20", "x50", "x30"] // so below is where we have some problem let randInt; let tempStr = ""; for (let i = 0; i < 10; i++) { // i want to make an array.length of 9 randInt = Math.floor( Math.random() * 3 ); tempStr = threeTicketArray[randInt]; // to find the "x30" or whichever i stored ticketArray.push(tempStr); // push it into the ticketArray let len = ticketArray.filter(val => val === tempStr) // we filter to bring out the once for this loops iteration if ( len.length > 3 ) { // if I have 4 of example "x30" ticketArray.pop(); // this should solve the infinit loop issue. // as it seems that randInt is generating dublicated. if (ticketArray.length <9) i-- } } // the end of the loop console.log(ticketArray);

about 4 years ago · Santiago Gelvez Relatório

0

Tu puedes hacer:

 const victoryMultipliers = ["x10", "x20", "x30", "x40", "x50", "x60"] const vmCopy = [...victoryMultipliers] const randomVM = arr => arr .splice(Math.floor(Math.random() * arr.length), 1) .pop() const ticketArray = [...Array(3)] .map(() => randomVM(vmCopy)) .reduce((a, c, i, arr) => a.concat(arr), []) .map(v => ({ v, s: Math.random() })) .sort((a, b) => as - bs) .map(({ v }) => v) console.log(ticketArray)

about 4 years ago · Santiago Gelvez 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