generatePassword(), похоже, возвращает пустой вывод.
Когда вы нажимаете кнопку «Создать пароль», он возвращает три пустых вывода.
Могу ли я узнать, почему это так?
Код: Выделить всё
Password Generator
Password Generator
Length of password:
Include uppercase letters
Include lowercase letters
Include symbols
Include numbers
Generate Password
1234
`
Код: Выделить всё
const passwordLengthCondition = document.getElementById("password-length");
const uppercaseCondition = document.getElementById("uppercase-condition");
const lowercaseCondition = document.getElementById("lowercase-condition");
const symbolsCondition = document.getElementById("symbols-condition");
const numbersCondition = document.getElementById("numbers-condition");
const conditionsChecklist = [
uppercaseCondition,
lowercaseCondition,
symbolsCondition,
numbersCondition,
];
function toIncludeCharacters() {
const uppercaseLetters = "QWERTYUIOPASDFGHJKLZXCVBNM";
const lowercaseLetters = "qwertyuiopasdfghjklzxcvbnm";
const symbols = "!@#$%^&*()_+-={}[]|\\:;\"',.?/~`";
const numbers = "1234567890";
const charactersList = [uppercaseLetters, lowercaseLetters, symbols, numbers];
let includedCharacters = ""; // Reset the characters for each call
for (let i = 0; i < conditionsChecklist.length; i++) {
if (conditionsChecklist[i].checked) {
includedCharacters += charactersList[i];
}
}
console.log(includedCharacters)
return includedCharacters;
}
function generatePassword(passwordLength) {
let password = "";
for (let i = 0; i < passwordLength; i++) {
password += toIncludeCharacters().charAt(Math.floor(Math.random() * includedCharacters.length));
}
console.log(password);
return password;
}
const generatePasswordButton = document.getElementById("create-password");
generatePasswordButton.addEventListener("click", () => {
const passwordLength = parseInt(passwordLengthCondition.value); // Convert to number
document.getElementById("generated-password").textContent = generatePassword(passwordLength);
console.log(generatePassword(passwordLength)); // Move this line here
});
const checklist = document.getElementsByClassName("form-check-input");
for (let i = 0; i < checklist.length; i++) {
checklist[i].addEventListener("change", toIncludeCharacters);
}
toIncludeCharacters();
generatePassword();
Код: Выделить всё
#generated-password {
margin: 3%;
font-weight: bolder;
font-size: larger;
}
body {
margin: 5%;
}
form {
background-color: aliceblue;
padding: 2%;
}
form div, button {
margin: 1%;
}
Подробнее здесь: https://stackoverflow.com/questions/768 ... iv-with-id
Мобильная версия