-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (69 loc) · 2.6 KB
/
script.js
File metadata and controls
88 lines (69 loc) · 2.6 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
77
78
79
80
81
82
83
84
85
86
87
88
// Assignment Code
var generateBtn = document.querySelector("#generate");
// Arrays
var typesOfCharacters = {
alphaLower: "abcdefghijklmnopqrstuvwxyz",
alphaUpper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
specialChar: "!#$%&'()*+,-./:;<=>?@[]^_`{|}~",
}
//variables
var confirmLower;
var confirmUpper;
var confirmSpecial;
var confirmNum;
// Write password to the #password input
function generatePassword() {
var value = prompt("How many characters would you like your password to be? Choose a length of at least 8 characters and no more than 128 characters.");
var length = parseInt(value);
// If user does not enter a correct length of characters value
if (length < 8 || length > 128) {
alert("Password must be between 8 and 128 characters.");
return "Please try again.";
}
if (!length) {
alert("Password must be between 8 and 128 characters");
return "Please try again.";
}
// At least one criteria should be chosen from below by the user
confirmLower = confirm("Will this contain lowercase letters?");
confirmUpper = confirm("Will this contain uppercase letters?");
confirmSpecial = confirm("Will this contain special characters?");
confirmNum = confirm("Will this contain numbers?");
var possibleCharacters = [];
// If no criteria is chosen by user
if (!confirmNum && !confirmSpecial && !confirmUpper && !confirmLower) {
alert("You must choose at least one of the following criteria: lowercase letters, uppercase letters, special characters, or numbers.");
return "Please try again";
}
//Start of user input criteria
if (confirmLower){
possibleCharacters = possibleCharacters.concat(typesOfCharacters.alphaLower.split(""));
}
if (confirmUpper){
possibleCharacters = possibleCharacters.concat(typesOfCharacters.alphaUpper.split(""));
}
if (confirmSpecial){
possibleCharacters = possibleCharacters.concat(typesOfCharacters.specialChar.split(""));
}
if (confirmNum){
for (var i = 0; i < 10; i++) {
possibleCharacters.push(i);
}
}
//Where password will be stored
var password = "";
//Randomizes the selections
for (var i = 0; i < length; i++) {
var randomIndex = Math.floor(Math.random() * possibleCharacters.length);
password = password + possibleCharacters[randomIndex];
}
return password;
}
//Generated password is sent to the page and is seen in text box
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);