Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!