Created
February 21, 2021 03:21
-
-
Save jimmypocock/fba7f5719978b3bd8589ec41b3bc0ba9 to your computer and use it in GitHub Desktop.
Nancy's Secrets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------// | |
// GLOBAL CONSTANTS | |
//----------------------------// | |
var UPPERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; | |
var LOWERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; | |
var NUMS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; | |
var SPECIALS = ['!', '#', '$', '%', '&', '(', ')', '*', '+', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@']; | |
var OPTIONS = [ | |
{ | |
prompt: 'Do you want lowercase letters in your password?', | |
set: LOWERS | |
}, | |
{ | |
prompt: 'Do you want upper case letters in your password?', | |
set: UPPERS | |
}, | |
{ | |
prompt: 'Do you want numbers in your password?', | |
set: NUMS | |
}, | |
{ | |
prompt: 'Do you want special characters in your password?', | |
set: SPECIALS | |
} | |
]; | |
//----------------------------// | |
// METHODS | |
//----------------------------// | |
function getLengthOfPassword() { | |
var length = prompt('Your password must be a number between 8 and 128.'); | |
length = parseInt(length, 10); | |
// EXTRA CREDIT: Validate to make sure only numbers are entered. Use regex? | |
if (!length) return; // NOTE: return so not stuck in recursive loop. | |
if (length < 8 || length > 128) { | |
alert("Your password must be between 8 and 128"); | |
length = getLengthOfPassword(); | |
} | |
return length; | |
} | |
function getCharacterSet() { | |
var characterSet = []; | |
for (var i = 0; i < OPTIONS.length; i++) { | |
var confirmation = confirm(OPTIONS[i].prompt); | |
if (confirmation) { characterSet = characterSet.concat(OPTIONS[i].set); } | |
} | |
// NOTE: This causes an endless loop if user never selects one. | |
if (characterSet.length === 0) { | |
alert('You must choose at least one.'); | |
getCharacterSet(); | |
} | |
return characterSet; | |
} | |
function getPassword(length, characterSet) { | |
var password = ''; | |
var setLength = characterSet.length; | |
for (var i = 0; i < length; i++) { | |
var index = Math.floor(Math.random() * setLength); | |
password += characterSet[index]; | |
} | |
return password; | |
} | |
function writePassword(password) { | |
document.querySelector('#password').value = password; | |
} | |
function generatePassword() { | |
var length = getLengthOfPassword(); | |
if (!length) return; | |
var characterSet = getCharacterSet(); | |
var password = getPassword(length, characterSet); | |
writePassword(password); | |
} | |
//----------------------------// | |
// EVENT LISTENERS | |
//----------------------------// | |
document.querySelector('#generate').addEventListener('click', generatePassword); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment