commit de840f111c0a0f217660dfd69e80100e7887efd2 Author: Rikuoh Tsujitani Date: Mon Nov 27 11:29:10 2023 +0900 OK diff --git a/password-generator.js b/password-generator.js new file mode 100644 index 0000000..78ac33d --- /dev/null +++ b/password-generator.js @@ -0,0 +1,15 @@ +'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());