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

265
Visualizações
Recurring fuction which reruns if a certain condition is not met in a loop

I need UsernameFn recursively called (cc later lines) until the conditional loop within which it is called becomes untrue. If I place the loop outside UsernameFn function entirely, console says Username is undefined, even though the var Username is declared globally. What am I supposed to do?

[It says to add more detail. I have no more detail. It says to add more detail. I have no more detail. It says to add more detail. I have no more detail. It says to add more detail. I have no more detail. ]

// dataset for username 

const alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var counter = 0;
var Username;

// determine random length of the username
var UNlength = Math.random() * 11;
UNlength = UNlength.toFixed(0);

function UsernameFn() {
  // generate random nums as pickers
  var PickerN = Math.random() * 10;
  PickerN = PickerN.toFixed(0);
  var PickerA = Math.random() * 26;
  PickerA = PickerA.toFixed(0);
  var PickerG = Math.random() * 10;
  PickerG = PickerG.toFixed(0);

  // Pickers for the first character in "Username"
  var PickerUN = Math.random() * 10;
  PickerUN = PickerUN.toFixed(0);
  var PickerUA = Math.random() * 26;
  PickerUA = PickerUA.toFixed(0);
  var PickerUG = Math.random() * 10;
  PickerUG = PickerUG.toFixed(0);

  // choose randomly btw num and alpha. If Math.random 
  // is an even num, option = alpha. If odd num, option = num
  if (PickerG % 2 == 0) {
    var Chosen = num[PickerN];
  } else {
    var Chosen = alpha[PickerA];
  }

  if (PickerUG % 2 == 0) {
    var Username = num[PickerUN];
  } else {
    var Username = alpha[PickerUA];
  }

  //append each new choice to a string of previous choices
  //when string = UNlength <= 10 chosen ranbdomly above, stop recursion.
  // 'Username' declaration outside scope to allow loop access
  Username = String(Username) + String(Chosen);
  return Username;

  counter += 1;
  do {
    UsernameFn()
  } while (Username.length <= UNlength.length);
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The loop should be outside the function. Then you need to concatenate the returned value to the Username variable.

Also, UNlength.length should just be UNlength.

// dataset for username 

const alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
const num = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var counter = 0;
var Username = '';

// determine random length of the username
var UNlength = Math.random() * 11;
UNlength = UNlength.toFixed(0);

do {
  Username += UsernameFn();
  counter++;
} while (Username.length <= UNlength);

console.log(Username, counter);

function UsernameFn() {
  // generate random nums as pickers
  var PickerN = Math.random() * 10;
  PickerN = PickerN.toFixed(0);
  var PickerA = Math.random() * 26;
  PickerA = PickerA.toFixed(0);
  var PickerG = Math.random() * 10;
  PickerG = PickerG.toFixed(0);

  // Pickers for the first character in "Username"
  var PickerUN = Math.random() * 10;
  PickerUN = PickerUN.toFixed(0);
  var PickerUA = Math.random() * 26;
  PickerUA = PickerUA.toFixed(0);
  var PickerUG = Math.random() * 10;
  PickerUG = PickerUG.toFixed(0);

  // choose randomly btw num and alpha. If Math.random 
  // is an even num, option = alpha. If odd num, option = num
  if (PickerG % 2 == 0) {
    var Chosen = num[PickerN];
  } else {
    var Chosen = alpha[PickerA];
  }

  if (PickerUG % 2 == 0) {
    var Username = num[PickerUN];
  } else {
    var Username = alpha[PickerUA];
  }

  //append each new choice to a string of previous choices
  //when string = UNlength <= 10 chosen ranbdomly above, stop recursion.
  // 'Username' declaration outside scope to allow loop access
  Username = String(Username) + String(Chosen);
  return Username;
}

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