I need to create an array from which random uppercase and lowercase alphabets can be used to generate a password. Could you tell me how to create such an array and add Math.random() to it?
Asked
Active
Viewed 36 times
-1
-
https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript – Marius Jun 20 '22 at 13:47
-
This answer can help you. https://stackoverflow.com/questions/9719570/generate-random-password-string-with-requirements-in-javascript – Mina Jun 20 '22 at 13:48
-
Please provide enough code so others can better understand or reproduce the problem. – Community Jun 20 '22 at 15:28
1 Answers
0
Users Marius and Mina commented on your question suggesting the answers from this post and this post -- which are valid and versatile answers. As an alternative, the following function returns a string of a fixed length containing random uppercase and lowercase letters -- without the use of a long string literal containing the alphabet:
function generateCode(length) {
var code = '';
for(var i = 0; i < length; i++) {
var character = String.fromCharCode(Math.floor(Math.random()*26)+97);
if(Math.floor(Math.random()*2) == 1) {
character = character.toUpperCase();
}
code+= character;
}
return code;
}
console.log(generateCode(12));
Hopefully this is useful to you, although the passcode it generates may not be particularly secure as a password. The questions linked above have additional comments and responses which contain more resources and answers to your question. Their answers may generate passwords with more security and randomness.

TGekko
- 155
- 4