I am trying to generate a random string of 5 OR 7 alphanumeric characters (Upper or lowercase)
Currently, I have:
var randStr = randomString(5);
but I am too novice to know how to make it generate either 5 or 7 characters in the string. I need it to randomly choose a 5 or 7 character string to generate.
I used
function randomString(string_length) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var output = "";
var index;
for (var i = 0; i < string_length; i++) {
var index = Math.floor(Math.random() * chars.length);
output += chars.substring(index, index + 1);
}
document.getElementById("current").innerHTML = output;
return output;
and
var randStr = randomString(5);
to get an output of a 5 character alphanumeric string but I need a 5 or a 7 randomly instead of switching it from a 5 to a 7. I want an either or.
3
Answers
it’s my code ,u can specify the length of the characters
Based on this great answer: