0

The assignment is to prompt for length of the password and character type from the user, then generate a random password. I think the for loop isn't working correctly. The retVal is returned empty because the for loop isn't passing it anything. I tried removing the charAt function and having the Math.floor give me just and index, that just gave me undefinedundefinedundefinedundefinedundefinedundefined. Back with the regular charAt function I'm getting nothing.

//ask for length
var length = prompt("How many characters will your password be? Enter a number between 8 and 128");

//ask for character type
var charType = prompt("Enter a character type: special, numeric, uppercase, lowercase.");

//generate password
function generatePassword() {
  //evaluate character type
  var charSet = "";
  if( charType.toLowerCase === "lowercase" ) {
    charSet = "abcdefghijklmnopqrstuvwxyz";
  } else if( charType.toLowerCase === "uppercase" ) {
    charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  } else if( charType.toLowerCase === "numeric" ) {
    charSet = "0123456789";
  } else if( charType.toLowerCase === "special" ) {
    charSet = " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
  } 
  //return value
  var retVal = "";
  //for (var i = 0, n = charSet.length; i < length; i++) {
    for (var i = 0, n = length; i < length; i++) {
    //picks a character within charSet at index of random number
    retVal += charSet.charAt(Math.floor(Math.random() * n));
  }
  console.log(retVal);
  return retVal;
}

3
  • Does this helps? stackoverflow.com/questions/1349404/… Commented Dec 27, 2019 at 18:12
  • Is there also a way to take an answer that fits multiple criteria and generate a password? Such as one that had uppercase letters and special characters? Commented Dec 27, 2019 at 21:42
  • Well yes, If you have multiple strings like "abc","ABC","1@3$" you could do something like password=abc[2]+ABC[1]+123[2] . This is just an example. You can randomize it using Math.random for indexes . Commented Dec 27, 2019 at 21:52

2 Answers 2

1

There are a couple of subtle issues you are having.

  • prompt returns a string, you will need to cast it to a number to use it for your length (Number(prompt(...))).
  • The string toLowerCase is a method, not a property, you have to call it (charType.toLowerCase()). You also only need to do this once, if you set it to a variable you can avoid re-computing it.
  • You want a random character in the full charset range, not the password length (using charSet.length).

var length = Number(prompt("How many characters will your password be? Enter a number between 8 and 128"));

//ask for character type
var charType = prompt("Enter a character type: special, numeric, uppercase, lowercase.");

//generate password
function generatePassword() {
  //evaluate character type
  var charSet = "";
  var charTypeLower = charType.toLowerCase();
  if( charTypeLower === "lowercase" ) {
    charSet = "abcdefghijklmnopqrstuvwxyz";
  } else if( charTypeLower === "uppercase" ) {
    charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  } else if( charTypeLower === "numeric" ) {
    charSet = "0123456789";
  } else if( charTypeLower === "special" ) {
    charSet = " !\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
  } 
  //return value
  var retVal = "";
  for (var i = 0; i < length; i++) {
    //picks a character within charSet at index of random number
    retVal += charSet.charAt(Math.floor(Math.random() * charSet.length));
  }
  return retVal;
}
alert(generatePassword());

Side Note:

I'm guessing this is just for learning purposes, but if you want to generate cryptographically secure passwords you should use a random number generator based on crypto.getRandomValues (see this question).

Sign up to request clarification or add additional context in comments.

2 Comments

I have a tiny DYI issue with your code. Can you please make a change to calculate charType.toLowerCase() only once?
@PM77-1 There's plenty of room for improvement but sure.
0

charType.toLowerCase is a function, what you want is charType.toLowerCase(), which is the result of the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.