Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- NOTE: If you want to run this program, you can do two different things.
- 1.) You can open the console in your browser (Ctrl + Shift + I in Google Chrome)
- and copy this code into it. Hit Enter, then type 'pwdGen()' (without the single quotes)
- and hit Enter again. The program will then run.
- 2.) Download a code editor or IDE (I used Notepad++ to make this. Here is the site: https://notepad-plus-plus.org/)
- Download or copy in both this code and another of my Pastebins entitled '<HTML> Random Password Generator (Javascript and HTML)'
- into separate documents. I have instructions for linking them together on my HTML.
- After everything is linked, click on the 'Run' drop-down menu and click on whichever browser you want to do.
- Then all you need to do is click the button, and the program will run.
- */
- function pwdGen () { //Defines a new function that is run using the button in the HTML section
- var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //Three lists that have all the characters that my password generator can use
- var numb = '1234567890';
- var punc = "`~!@#$%^&*()_+-=}{[];'\":,./<>?\|";
- var possibleChars = alpha + numb; //Combines the alpha and numb strings above
- function nextChar(chars) { //Defines a new function that will randomly select characters from the strings
- var loc = Math.round(Math.random()*chars.length - 1);
- /*Creates a variable loc and gives it the value of a random number
- (look Math.random() and Math.round() up on your own) which is multiplied
- by the length of the string (created by combining the original strings)*/
- return chars.substring(loc, loc + 1); //uses the number in loc to take one character out of the combined strings
- };
- /*Makes a pop-up to ask if the user wants to use the program*/
- permissionRandGen = confirm('Would you like a random password suggestion?');
- if (permissionRandGen == true) { //If the user wants to use the program, this starts to generate the function
- var lenPwd = prompt('How long do you want your password?'); //Asks for length of the password
- var usePunc = confirm('Do you want punctuation in your password?'); //Asks the user if they want characters
- /*If the user want punctuation, adds it to the possible characters*/
- if (usePunc == true) {
- possibleChars = possibleChars + punc;
- };
- /*If the length is between 1 and 25, the program will run*/
- if (lenPwd > 0 && lenPwd < 26) {
- var newPwd = ""; //Creates a variable the will store the password
- /*Creates new characters until the password it complete*/
- for (var i = 0; i < lenPwd; i++) {
- newPwd += nextChar(possibleChars);
- };
- /*Creates a new element in the HTML section, adds text to it, and then adds the element to <body>*/
- /*Shows the user their new password suggestion*/
- var docElementNewPwd = document.createElement("p")
- var docTextNewPwd = document.createTextNode("Your new password is: " + newPwd);
- docElementNewPwd.appendChild(docTextNewPwd);
- document.body.appendChild(docElementNewPwd);
- /*NOTE: If the program is run multiple times without refreshing the page, you will be able to see all of the passwords generated*/
- /*If the password length is longer or shorter than the limit, this informs the user*/
- } else {
- var dELenError = document.createElement("p");
- var dTLenError = document.createTextNode("I can only generate passwords between 1 and 25 characters long.");
- dELenError.appendChild(dTLenError);
- document.body.appendChild(dELenError);
- };
- /*If the user chooses not to run the program, this creates a heading (<h2>) element to ask a rhetorical question*/
- } else {
- var dEUserEnd = document.createElement("h2");
- var dTUserEnd = document.createTextNode("Well then, why did you run me?");
- dEUserEnd.appendChild(dTUserEnd);
- document.body.appendChild(dEUserEnd);
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement