// Function to generate a random password of specified length
function generateRandomPassword(length) {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
let password = '';
// Loop to randomly select characters from the charset
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
return password;
}
// Usage: Generate a random password with a length of 12
let randomPassword = generateRandomPassword(12);
code
Generate Random Password in JavaScript
This JavaScript code defines a function generateRandomPassword to generate a random password using a specified character set and length. The function is then called to generate a random password with a length of 12. The comments provide explanations for each part of the code.
Random Password
Character Set