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;
}
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • Does this helps? https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript – Saurav Dec 27 '19 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? – May Hitchings Dec 27 '19 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 . – Saurav Dec 27 '19 at 21:52

2 Answers2

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).

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
0

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

jefi
  • 185
  • 1
  • 13