JavaScript program to generate random string

Views: 181   

Following program shows you how to generate random string.
This program gets string length from user and prints random string of the given length

var stringLength = prompt("Please enter the string length:")
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var result = '';
for (var i = 1; i <= stringLength; i++) {
result = result + alphabet.charAt(Math.floor(Math.random() * alphabet.length))
}
console.log("Random string is: " + result);

Output:

Please enter the string length:
10
AwcNqKwhil
On By