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

357
Visualizações
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 Respostas
Responde à pergunta

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