Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- let generatePassword = function(passwordLength = 10) {
- let alphabetString = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-_=+[]{}!@#$%^&*();:'|,<.>/?`",
- alphabet = alphabetString.split("",),
- passwordArray = [],
- getRandomSymbol = function() {
- return alphabet[Math.floor(Math.random()*alphabet.length)]
- };
- for (let i = 0; i < passwordLength; i++) {
- passwordArray.push(getRandomSymbol());
- }
- let password = passwordArray.join(""),
- passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
- if (password.match(passwordStrengthCheck)) {
- return console.log(`Your password is: ${password}`);
- } else {
- console.log("Failed to generate a strong enough password");
- }
- }
- // Function call sample:
- generatePassword();
- let generatePassword = function(passwordLength = 10) {
- let capitals = "QWERTYUIOPASDFGHJKLZXCVBNM";
- let small = "qwertyuiopasdfghjklzxcvbnm";
- let digits = "1234567890";
- let symbols = "-_=+[]{}!@#$%^&*();:'|,<.>/?`",
- let all = capitals + small + digits + symbols;
- getRandomSymbol = function(sourceString) {
- return sourceString[Math.floor(Math.random()*sourceString.length)]
- };
- alphabet = alphabetString.split("",),
- passwordArray = [],
- passwordArray.push(getRandomSymbol(capitals));
- passwordArray.push(getRandomSymbol(small));
- passwordArray.push(getRandomSymbol(digits));
- passwordArray.push(getRandomSymbol(symbols));
- for (let i = 0; i < passwordLength - 4; i++) {
- passwordArray.push(getRandomSymbol());
- }
- shuffle = function (a) {
- for (let i = a.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- [a[i], a[j]] = [a[j], a[i]];
- }
- return a;
- }
- let password = shuffle(passwordArray).join(""),
- passwordStrengthCheck = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
- if (password.match(passwordStrengthCheck)) {
- return console.log(`Your password is: ${password}`);
- } else {
- console.log("Failed to generate a strong enough password");
- }
- }
Add Comment
Please, Sign In to add comment