Hey guys I have been working on a student project which is build a random password generator I have completed the code to the best of my ability doing lots of looking into youtube videos and with teachers' notes I can not seem to get it to print the end result. Any help would be appreciated. I am still new to coding so go easy :)
var generateBtn = document.querySelector("#generate");
// addEventListener to button
generateBtn.addEventListener("click", writePassword);
let arrayFromLowtoHighNum = (low, high) => {
const array = [];
for (let i = low; i <= high; i++) {
array.push(i);
}
return array;
};
const UPPERCASE_CODES = arrayFromLowtoHighNum(65, 90);
const LOWERCASE_CODES = arrayFromLowtoHighNum(97, 122);
const NUMBER_CODES = arrayFromLowtoHighNum(48, 57);
const SYMBOLS_CODES = arrayFromLowtoHighNum(33, 47)
.concat(arrayFromLowToHighNum(58, 64))
.concat(arrayFromLowToHighNum(91, 96))
.concat(arrayFromLowToHighNum(123, 126));
// Choose character length
function generatePassword() {
var passwordLength = window.prompt("Choose a password length between 8 & 128")
if (parseInt(passwordLength) >= 8 && parseInt(passwordLength) <= 128) {} else {
// window alert
window.alert("You did not enter a valid number, please try again.");
return
}
// Password variables
var lowerCase = window.confirm("Would you like to use lower case characters?");
var upperCase = window.confirm("Would you like to use upper case characters?");
var numbers = window.confirm("Would you like to use numbers?");
var specialCharacters = window.confirm("Would you like special characters?");
// Password response
var lowerCaseAnswer = 0;
if (lowerCase === true) {
lowerCaseAnswer = true
} else {
lowerCaseAnswer = false
}
var upperCaseAnswer = 0;
if (upperCase === true) {
upperCaseAnswer = true
} else {
upperCaseAnswer = false
}
var nummberAnswer = 0;
if (number === true) {
nummberAnswer = true
} else {
nummberAnswer = false
}
var specialCharactersAnswer = 0;
if (specialCharacters === true) {
specialCharactersAnswer = true
} else {
specialCharactersAnswer = false
}
let charCode = [];
if (lowerCaseAnswer == true) {
charCodes = charCode.concat(LOWERCASE_CODES);
}
if (upperCase == true) {
charCodes = charCode.concat(UPPERCASE_CODES);
}
if (specialCharacters == true) {
charCodes = charCode.concat(SYMBOLS_CODES);
}
if (nummberAnswer == true) {
charCodes = charCode.concat(NUMBER_CODES);
}
const passwordCharacters = [];
for (let i = 0; i < passwordLengt; i++) {
const characterCode = charCode[Math.floor(Math.random() * charCode.length)];
passwordCharacters.push(String.fromCharCode(characterCode));
console.log(characterCode);
}
return passwordCharacters.join('');
}
// Parse password to #password input
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
console.log(password);
}
<buttun type="button" id="generate">Generate</button>
Many issues
Mostly differences between variable names and spelling mistakes
This works
const arrayFromLowToHighNum = (low, high) => {
const array = [];
for (let i = low; i <= high; i++) {
array.push(i);
}
return array;
};
const UPPERCASE_CODES = arrayFromLowToHighNum(65, 90);
const LOWERCASE_CODES = arrayFromLowToHighNum(97, 122);
const NUMBER_CODES = arrayFromLowToHighNum(48, 57);
const SYMBOLS_CODES = arrayFromLowToHighNum(33, 47)
.concat(arrayFromLowToHighNum(58, 64))
.concat(arrayFromLowToHighNum(91, 96))
.concat(arrayFromLowToHighNum(123, 126));
// Choose character length
function generatePassword() {
var passwordLength = +window.prompt("Choose a password length between 8 & 128")
if (passwordLength < 8 || passwordLength > 128) {
window.alert("You did not enter a valid number, please try again.");
return "";
}
// Password variables
var lowerCase = window.confirm("Would you like to use lower case characters?");
var upperCase = window.confirm("Would you like to use upper case characters?");
var numbers = window.confirm("Would you like to use numbers?");
var specialCharacters = window.confirm("Would you like special characters?");
let charCodes = [];
if (lowerCase) {
charCodes = charCodes.concat(LOWERCASE_CODES);
}
if (upperCase) {
charCodes = charCodes.concat(UPPERCASE_CODES);
}
if (specialCharacters) {
charCodes = charCodes.concat(SYMBOLS_CODES);
}
if (numbers) {
charCodes = charCodes.concat(NUMBER_CODES);
}
if (charCodes.length === 0) {
alert("Please choose at least one set of characters");
return ""
}
const passwordCharacters = Array.from({length: passwordLength}, () => {
const characterCode = charCodes[Math.floor(Math.random() * charCodes.length)];
return String.fromCharCode(characterCode);
})
return passwordCharacters.join('');
}
// Parse password to #password input
function writePassword() {
const password = generatePassword();
document.getElementById("password").value = password;
}
document.getElementById("#generate").addEventListener("click", writePassword);
<button type="button" id="generate">Generate</button>
<input id="password">