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

260
Vistas
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 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