I’m trying to create a password generator for a class assignment. Every time I go through the prompt windows and select my password info I get "undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined."
I’ve been staring at this for 3 hours trying to figure it out and I’m convinced the longer I stare at it the less I know about JS. Every time I change something I make it worse. What am I doing wrong?
// Assignment Code
passwordLength = 8;
var generateBtn = document.querySelector("#generate");
var finalPassword = [];
var lowerCase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var upperCase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'z', ];
var characters = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', ',', '.', '?', '<', '>', '?'];
var numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
// Write password to the #password input
generateBtn.addEventListener("click", writePassword);
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
function generatePassword() {
var randomPassword = "";
for (let i = 0; i < passwordLength; i++) {
let randomizer = Math.floor(Math.random() * Math.floor(passwordLength.length));
randomPassword = +finalPassword[randomizer];
}
return randomPassword;
}
function promptWindow() {
finalPassword = [];
passwordLength = parseInt(prompt("Select a password length between 8 - 128 characters"));
if (isNaN(passwordLength) || passwordLength <= 8 || passwordLength >= 128) {
alert("WARNING! Invalid password length entered");
return false;
}
if (confirm("Include uppercase letters in password?")) {
finalPassword = finalPassword.concat(upperCase);
}
if (confirm("Include special characters in password?")) {
finalPassword = finalPassword.concat(characters);
}
if (confirm("Include numbers in password?")) {
finalPassword = finalPassword.concat(numbers);
}
return true;
}
2
Answers
I suspect that your problem is here on this line in
generatePassword
:I think you’re mistakenly using the variable
passwordLength
, which is a Number, so it doesn’t have a.length
property. So when you runMath.floor(passwordLength.length)
, you’re really runningMath.floor(undefined)
. If you run that in a console, you will getNaN
back. Usually any math operation withNaN
results inNaN
again. So the result of all your math on that line is this:On the next line, you’re asking for the element in the Array
finalPassword
at theNaN
th position, which doesn’t exist, so it’sundefined
.Change that line in the
generatePassword
function to:(
finalPassword.length
is always an integer, so you don’t need to callMath.floor()
on it.)That might help clear up at least some of the issues. Hope that works!
This will return undefined (passwordLength is a number)
also need to change =+ to += on the line
here is a peice of code generating a random password based on your initial code.