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

257
Views
Función recurrente que se vuelve a ejecutar si no se cumple una determinada condición en un bucle

Necesito que se llame a UsernameFn recursivamente (cc líneas posteriores) hasta que el ciclo condicional dentro del cual se llama se vuelva falso. Si coloco el bucle fuera de la función UsernameFn por completo, la consola dice que el nombre de usuario no está definido, aunque el nombre de usuario var se declare globalmente. ¿Que se supone que haga?

[Dice para agregar más detalles. no tengo mas detalle Dice que agregue más detalles. no tengo mas detalle Dice que agregue más detalles. no tengo mas detalle Dice que agregue más detalles. no tengo mas detalle ]

 // 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 answers
Answer question

0

El ciclo debe estar fuera de la función. Luego, debe concatenar el valor devuelto a la variable Username de usuario.

Además, UNlength.length debería ser simplemente 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 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!