Hello! Here is my code for the random password generator, it works by doing the following;
It begins by attaching a querySelector to the id ''#generate''. When the user clicks on the ''generate password'' button it begins the writePassword function. This function by itself would not do anything however the following function generatePassword begins collecting data from the user to eventually use in a randomized password.
The first variable of the password is the actual length. The code sets up the parameter for this by using parseInt. This function essentially requires that the following input be a number otherwise it will return NaN (not a number). The prompt function appears in the window and informs the user of the password length requirements.
The lenght requirements for this password is between 6 and 128, if the user puts in a number outside of this range they will be displayed with an appropriate error message and the function will end. If they put in a correct number the function will contine with a series of yes or no questions asking if the user would also like their password to include capital letters/ lowercase letters, numbers, special characters, and for this particuler code emojis.
Up next we have the charset variable which is = "". It is left blank as this will be the range of units the code uses to determine the final password.
Followed by that is the var password = "". The first thing it does is start a for loop with the aforementioned data using a standard code of (var i = 0; i < length; i++) which tells the coding process (which comes next in the code) to start at the first possible entry and to then run the length of the numeric input entered in the beginning and to continue to run that length until it gets to an entry that would exceed that limit.
Next is in my opinion the most interesting/critical part of the code and that is how it takes all of the previous input parameters and randomizes them into our final code. It starts with a randomIndex variable which will be the placeholder for our final password later. It then does a Math.random function along with the length of the charset and provides a list of potential passwords before deciding on one. My understanding is that it chooses the last entry before the one that would cause the random generated number entries to exceed the total selected input characters, but I could be mistaken on this. The | 0; works as a fail safe should the user enter a length number with a decimal. This entry would cause it to round down.
The next line of code is telling java to take the result of the randomIndex variable and set it as the charset value, which will then be set for our 'password' string. The CharAt function is telling the charset to look at the following value, which is in this case randomIndex and return it to be used for our password which is done so with the return command.
Now its time for the last part that pulls everything together. This is first triggered by the ''click'' action at the top of the code which triggers the writePassword function. The var password = generatePassword is telling the code to take the final result of the collected and randomized parameters and set that as our password. The next line is telling the HTML file that the code will be put in the place of the #password ID. Then last but not least the final line is telling java to replace the #password text with the result of our generatePassword function.
I hope that this explanation makes sense and if I am incorrect or misinformed on what a process is doing please correct me. Thank you!
Justin Hodges
