skip to Main Content

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


  1. I suspect that your problem is here on this line in generatePassword:

    let randomizer = Math.floor(Math.random() * Math.floor(passwordLength.length));
    

    I think you’re mistakenly using the variable passwordLength, which is a Number, so it doesn’t have a .length property. So when you run Math.floor(passwordLength.length), you’re really running Math.floor(undefined). If you run that in a console, you will get NaN back. Usually any math operation with NaN results in NaN again. So the result of all your math on that line is this:

    let randomizer = NaN;
    randomPassword += finalPassword[randomizer];
    

    On the next line, you’re asking for the element in the Array finalPassword at the NaNth position, which doesn’t exist, so it’s undefined.

    Change that line in the generatePassword function to:

    let randomizer = Math.floor(Math.random() * finalPassword.length);
    

    (finalPassword.length is always an integer, so you don’t need to call Math.floor() on it.)

    That might help clear up at least some of the issues. Hope that works!

    Login or Signup to reply.
  2. This will return undefined (passwordLength is a number)

    Math.floor(passwordLength.length)
    

    also need to change =+ to += on the line

    randomPassword = +finalPassword[randomizer];
    

    here is a peice of code generating a random password based on your initial code.

    // Assignment Code
    // let passwordLength = 8;
    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'];
    var generateBtn = document.querySelector("#generate");
    // var finalPassword = [];
    
    // Write password to the #password input
    generateBtn.addEventListener("click", writePassword);
    function writePassword() {
        var password = promptWindow();
        var passwordText = document.querySelector("#password");
        passwordText.value = password;
    }
    
    
    function generatePassword(types, len) {
        var randomPassword = "";
        
        for (let i = 0; i < len; i++) {
    
            //pick type
            let random_type = types[Math.floor(Math.random() * types.length)] ;
            //pick character
            let random_char = random_type[Math.floor(Math.random() * random_type.length)]
    
            randomPassword += random_char;
        }
        return randomPassword;
    }
    
    function promptWindow() {
        finalPassword = [];
        passwordLength = parseInt(prompt("Select a password length between 8 - 128 characters"));
        let types =  [lowerCase]
        
    
        if (isNaN(passwordLength) || passwordLength <= 8 || passwordLength >= 128) {
            alert("WARNING! Invalid password length entered");
            return "";
        }
        if (confirm("Include uppercase letters in password?")) {
            // finalPassword = finalPassword.concat(upperCase);
            types.push(upperCase)
        }
        if (confirm("Include special characters in password?")) {
            // finalPassword = finalPassword.concat(characters);
            types.push(characters)
        }
        if (confirm("Include numbers in password?")) {
            // finalPassword = finalPassword.concat(numbers);
            types.push(numbers)
        }
    
        return generatePassword(types,passwordLength);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search