I wanted my passwords to include at least: 1 capital, 1 special character I am trying with this while loop to meet the requirements. What am i doing wrong?
const characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#$!-_";
const capital = /ABCDEFGHIJKLMNOPQRSTUVWXYZ/;
const special = /#$!-_/;
function PWord (){
let pass = ""
while(!((capital.test(pass)) && (special.test(pass))) ){
for (i=0; i<=11; i++)
{
let rand_char = characters.charAt(Math.floor(Math.random()*characters.length));
pass = pass + rand_char;
}
return pass
}
}
for(let i=0;i<=3;i++){
let ps = PWord();
console.log("Password" + (i+1) + ":" + ps);
}
The main issue I see here is your use of return in the while loop. return will always end the execution of the entire function and return the specified value. The condition in your loop will only be checked once since you're returning before giving it the chance to check and run again. Try moving that line outside of the loop.