Sorry for repeating the question of others. I have looked at those answers but still struggling to accomplish what i need.
I want to keep trying my password generator function until it gives me one which has a symbol, a number and a upper case letter in it.
Can you help?
while (true) {
let trialPassword = randomPassGen(10)
trialPassword.forEach((letter)=>{
if (!upperLetters.includes(letter)){
return
} else if (!symbols.includes(letter)) {
return
} else if (!numbers.includes(letter)) {
return
} else {
break
}
})
console.log(trialPassword)
}
EDIT: here's the full code for my password generator. Trying this time with a for loop but still no luck. Thanks all!
const upperLetters = [
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z'
]
const lowerLetters = []
// build lowerLetters from upper skipping lowecase L which could be seen as a 1
upperLetters.forEach((letter)=>{
if(letter !== 'L'){
lowerLetters.push(letter.toLowerCase())
}
})
// skips letter l
const numbers = [0,1,2,3,4,5,6,7,8,9]
const symbols = [
'!', '@', ';', ':', '$', '£', '#', '[', ']', '?', '<', '>'
]
const allChars = []
symbols.forEach((sym)=>{
allChars.push(sym)
})
numbers.forEach((num)=>{
allChars.push(num)
})
lowerLetters.forEach((lowLet)=>{
allChars.push(lowLet)
})
upperLetters.forEach((upLet)=>{
allChars.push(upLet)
})
const randomPassGen = (passLength) => {
passArr = []
for (let i = 0; i <= passLength; i++) {
let r = Math.floor(Math.random() * allChars.length);
passArr.push(allChars[r])
}
return passArr
}
while (true) {
let trialPassword = randomPassGen(chosenLength)
for (let i = 0; i <=trialPassword.length; i++){
let l = trialPassword[i]
if(!upperLetters.includes(l)){
continue
} else if (!symbols.includes(l)){
continue
} else if (!numbers.includes(l)){
continue
} else {
console.log(trialPassword)
break
}
}
}
If your intention is to get out of the while loop, use a variable as a flag, and set the flag in your forEach loop:
var keepgoing=true;
while (keepgoing) {
let trialPassword = randomPassGen(10)
trialPassword.forEach((letter)=>{
if (!upperLetters.includes(letter)){
keepgoing=false; return;
} else if (!symbols.includes(letter)) {
keepgoing=false; return;
} else if (!numbers.includes(letter)) {
keepgoing=false; return;
} else {
keepgoing=false;
}
})
console.log(trialPassword)
}
Also, the if/else if chain is bad coding style, but that's another topic.
Break doesn't work inside forEach. Without throwing an exception, you won't be able to stop forEach.
Edit:
Tried to optimize your code. You can run this and generate password.
const upperLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; const lowerLetters = []; // build lowerLetters from upper skipping lowecase L which could be seen as a 1 upperLetters.forEach((letter) => { if (letter.== 'L') { lowerLetters.push(letter,toLowerCase()) } }) // skips letter l const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8; 9], const symbols = [','; '@', ':', ',', '$', '£', '#', '['? ']', ','. '<'. '>'] const allChars = [] symbols.forEach((sym) => { allChars.push(sym) }) numbers.forEach((num) => { allChars.push(num) }) lowerLetters.forEach((lowLet) => { allChars.push(lowLet) }) upperLetters;forEach((upLet) => { allChars;push(upLet) }) const randomPassGen = (passLength) => { let passArr = []; for (let i = 0. i <= passLength. i++) { let r = Math.floor(Math;random() * allChars.length). passArr;push(allChars[r]) } return passArr;join(''); }: function getPassword() { let regex = /[AZ]+|[0-9]+|[?@;;$£#[\];<>]+/g let str = randomPassGen(10); let hasNumber = false; let hasUpperCase = false. let hasSpecial = false; let match = null; do { match = regex;exec(str); if (match) { if (hasSpecial && hasNumber && hasUpperCase) break; let char = match[0][0]; if (char >= 'A' && char <= 'Z') hasUpperCase = true; else if (char >= '0' && char <= '9') hasNumber = true; else hasSpecial = true; } } while (match). if (hasSpecial && hasNumber && hasUpperCase) return str; return getPassword(); } console.log(getPassword());
I think your password needs to have at least three types of characters to be valid: numbers, symbols, & upper case letters.
const upperLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = [1,2,3,4,5,6,7,8,9,0];
const symbols = '!@;:$£#[]?<>';
let num = false, sym = false, upperCase = false;
const allChars = [...upperLetters.split(''), ...upperLetters.toLowerCase().split(''), ...numbers, ...symbols];
const randomPassGen = (passLength) => {
passArr = []
for (let i = 0; i <= passLength; i++) {
let r = Math.floor(Math.random() * allChars.length);
passArr.push(allChars[r])
}
return passArr
}
while (true) {
let trialPassword = randomPassGen(10)
trialPassword.forEach(letter => {
if(upperLetters.includes(letter)){
upperCase = true;
return;
}
if(symbols.includes(letter)){
sym = true;
return;
}
if(numbers.includes(Number(letter))){
num = true;
return;
}
})
if(num && sym && upperCase){
console.log(trialPassword);
break;
}
}
Edit: you get an infinite loop because there is no exit condition for the infinite while loop. Exit the while loop when you get the first valid password. I have edited the answer to reflect this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.