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

261
Visualizações
How to immediately shuffle numbers from the results of an API call?

This is definitely a beginner problem, but I can’t figure it out or find an answer anywhere, so I’m hoping someone here can tell me what I’m doing wrong without too much bother. I'm making a call to an API, getting a series of ID numbers corresponding to categories back that I'm putting into an array. I want to then shuffle the numbers in that array so I can use the ID numbers in a random order easily later on. When I run this code I get an error that the return statement at the end is an illegal return statement. If I run it without the stuff with the counter and my attempt at shuffling, there's no error.

async function getCategoryIds() {
    let idArray = [];
    let fullCatInfoArray = [];
    let response = await axios.get('API URL');

    for (let cats of response.data) {
        idArray.push(cats.id);
    }

    let counter = idArray.length;

    while (counter > 0) {
      let index = Math.floor(Math.random() * counter);
    
      counter--;
    
      let temp = idArray[counter];
      idArray[counter] = idArray[index]; 
      idArray[index] = temp; 
    }    
  
    return idArray;
}
about 4 years ago · Juan Pablo Isaza
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