很抱歉重复其他人的问题。我已经查看了这些答案,但仍在努力完成我所需要的。
我想继续尝试使用密码生成器功能,直到它提供一个带有符号,数字和大写字母的密码。
你能帮忙吗?
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)
}
编辑:这是我的密码生成器的完整代码。这次尝试使用for循环,但还是没有运气。谢谢大家!
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
}
}
}
答案 0 :(得分:1)
如果您打算退出while循环,请使用变量作为标志,并在forEach循环中设置标志:
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)
}
此外,if / else if链的编码风格也不佳,但这是另一个主题。
答案 1 :(得分:0)
断点在forEach中不起作用。不抛出异常,您将无法停止forEach。
编辑:
试图优化您的代码。您可以运行它并生成密码。
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 = /[A-Z]+|[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());
答案 2 :(得分:0)
我认为您的密码至少需要三种字符才能有效:数字,符号和大写字母。
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;
}
}
编辑:您会得到一个无限循环,因为无限while循环没有退出条件。当您获得第一个有效密码时,退出while循环。我已经编辑了答案以反映这一点。