No additional packages needed
JavaScript:
// Password generator function
function generatePassword(length = 12, options = { lowercase: true, uppercase: true, numbers: true, symbols: true }) {
// Define character sets
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const symbols = '!@#$%^&*()_+[]{}|;:,.<>?';
// Create a pool of characters based on the selected options
let characters = '';
if (options.lowercase) characters += lowercase;
if (options.uppercase) characters += uppercase;
if (options.numbers) characters += numbers;
if (options.symbols) characters += symbols;
// Generate the password
let password = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters[randomIndex];
}
return password;
}
// Example usage: Generate a 16-character password with all character types
const password = generatePassword(16, { lowercase: true, uppercase: true, numbers: true, symbols: true });
console.log('Generated Password:', password);