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; }
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(...))
). 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.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());
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 ).
charType.toLowerCase
是一个函数,你想要的是charType.toLowerCase()
,它是函数的结果。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.