-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (57 loc) · 2.75 KB
/
Copy pathscript.js
File metadata and controls
76 lines (57 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// PASSWORD GENERATOR using 8-128 characters based on user input.
// --------------------------------------------------------------
// Character Variables:
const upperCase = "ABCDEFGHIJKLMNOPQRSTUZWXYZ";
const lowerCase = "abcdefghijklmnopqrstuvwxyz";
const num = "0123456789";
const symbols = "!#$%&'()*+,-./:;<=>?@[^_`{|}~";
// Query Selectors
let passwordTextarea = document.querySelector("#password");
let generateButton = document.querySelector("#generate");
// Event Listeners
generateButton.addEventListener("click", generatePassword);
//User input with prompts for incorrect data entry.
function generatePassword() {
let newPassword = "";
let characters = "";
let passwordLength = prompt("How many characters long would you like your password to be? (Min. 8 / Max. 128)");
// isNan function researched at MDN Web Docs[ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN ] Discovered at Travrsy Media YouTube tutoral [ https://www.youtube.com/watch?v=hdI2bqOjy3c ].
if (Number(passwordLength < 8) || Number(passwordLength > 128) || isNaN(passwordLength)) {
alert("Must be between 8 and 128 characters.");
return;
}
// Uppercase option:
let upperCaseResponse = confirm("Would you like your password to contain UPPERCASE letters? (Choose 'OK' for Yes, 'Cancel' for No.)");
if (upperCaseResponse) {
characters += upperCase;
}
// Lowercase option:
let lowerCaseResponse = confirm("Would you like your password to contain lowercase letters? (Choose 'OK' for Yes, 'Cancel' for No.)");
if (lowerCaseResponse) {
characters += lowerCase;
}
// Numbers option:
let numResponse = confirm("Would you like your password to contain numbers? (Choose 'OK' for Yes, 'Cancel' for No.)");
if (numResponse) {
characters += num;
}
// Special symbols option:
let symbolsResponse = confirm("Would you like your password to contain spec!@l ch@r&cters? (Choose 'OK' for Yes, 'Cancel' for No.)");
if (symbolsResponse) {
characters += symbols;
}
// If no options are chosen:
if (characters === "") {
alert("You must choose at least one character type. Try again.");
}
// For loop to generate password
if (Number(passwordLength >= 8) && Number(passwordLength <= 128)) {
for (var i = 0; i < passwordLength; i++) {
newPassword += characters.charAt(Math.floor(Math.random() * characters.length));
}
// Local Storage researched at dcode YouTube tutorial [ https://www.youtube.com/watch?v=k8yJCeuP6I8 ].
localStorage.setItem("password", newPassword);
let newPasswordString = localStorage.getItem("password");
passwordTextarea.textContent = newPasswordString;
}
}