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

356
Views
Function declared in a loop contains unsafe references to variable(s)

My code checks if a user is available. See snippet below:

const users = ['user1', 'user2', 'user3', 'user4']
const usersToAdd = 2

const getRandomWorker = (userArray) => {
  return userArray[Math.floor(userArray.length * Math.random())]
}

const availableUsers = []

for (let i = 0; i < usersToAdd; i += i) {
  let randomWorker = getRandomWorker(users)
  let didAddWorker = false

  while (!didAddWorker) {
    if (checkIfUserAvailable(randomWorker)) {
      availableUsers.push(randomWorker)
      users = users.filter((user) => user !== randomWorker)
      didAddWorker = true
    } else if (!users.length) {
      didAddWorker = true
    } else {
      users = users.filter((user) => user !== randomWorker)
    }
  }
}

My only problem is that it contains unsafe references to variables(s) because I get the following error:

Function declared in a loop contains unsafe references to variable(s) randomWorker.

I've searched around and fiddled with my code but I can't get rid of the error. I don't know where to look anymore.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As T.J Crowder suggested I had to make a function outside of the loop:

const filterOut = (array, target) => array.filter(element => element !== target);

That fixed it. A bit strange but I can continue know. Thanks!

about 4 years ago · Juan Pablo Isaza Report
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!