password-generator/password-generator.js

16 lines
509 B
JavaScript
Raw Normal View History

2023-11-27 11:29:10 +09:00
'use strict';
const length = 20;
2023-11-27 11:30:32 +09:00
const charset = 'abcdefghijklmnopqrstuvwxyz' + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + '0123456789' + '!@#$%^&*+?{}()';
2023-11-27 11:29:10 +09:00
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());