password-generator/password-generator.js
2023-11-27 11:30:32 +09:00

15 lines
509 B
JavaScript

'use strict';
const length = 20;
const charset = 'abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + '0123456789' + '!@#$%^&*+?{}()';
function passwordGenerator() {
let password = '';
for (let i = 0; i < length; i++) {
password += charset[Math.floor(Math.random() * charset.length)];
}
const includeAllTypes =
/[a-z]/.test(password) && /[A-Z]/.test(password) && /[0-9]/.test(password);
return includeAllTypes ? password : passwordGenerator();
}
console.log(passwordGenerator());