-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (109 loc) · 4.1 KB
/
script.js
File metadata and controls
146 lines (109 loc) · 4.1 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Assignment Code
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
var userLengthselection = prompt("Please enter the length of your password, must be at least 8 and no more than 128 characters");
var userLength = parseInt(userLengthselection);
while (userLength > 128 || userLength < 8) {
alert("Please try again, must be at least 8 characters but no more than 128");
userLengthselection = prompt("Please enter the length of your password, must be at least 8 and no more than 128 characters");
userLength = parseInt(userLengthselection);
}
alert(userLength);
var uppercase = false;
var lowercase = false;
var numeric = false;
var specialcharacters = false;
var errorCheck = 0;
while (uppercase == false && lowercase == false && numeric == false && specialcharacters == false) {
if (errorCheck > 0) {
alert("please enter a valid selection")
}
var userCharacterselection = prompt("What type of characters do you want? uppercase, lowercase, numeric, special characters? Type all that you want");
var userLC = userCharacterselection.toLowerCase();
if (userLC.search("uppercase") !== -1){
uppercase = true;
}
if (userLC.search("lowercase") !== -1){
lowercase = true;
}
if (userLC.search("numeric") !== -1){
numeric = true;
}
if (userLC.search("special characters") !== -1){
specialcharacters = true;
}
errorCheck = errorCheck + 1;
}
var userMessage = "you have selected the following criteria: ";
if (uppercase == true) {
userMessage = userMessage + "uppercase ";
}
if (lowercase == true) {
userMessage = userMessage + "lowercase ";
}
if (numeric == true) {
userMessage = userMessage + "numeric ";
}
if (specialcharacters == true) {
userMessage = userMessage + "special characters ";
}
alert(userMessage);
var password = generatePassword(userLength, uppercase, lowercase, numeric, specialcharacters);
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);
function generatePassword(length, upper, lower, numeric, specials) {
// Possible characters
var characters_for_upper = Array.from('ABCDEFGHIJKLMPNOPQRSTUVWXYZ');
var characters_for_lower = Array.from('abcdefghijklmnopqrstuvwxyz');
var characters_for_numeric = Array.from('0123456789');
var characters_for_specials = Array.from('!@#$%^&*()-_=+]}[{:.;,/|}]');
// Will hold randomly generated characters of each type
var uppercase_string = [];
var lowercase_string = [];
var numeric_string = [];
var specials_string = [];
// Generate randomly characters of the max possible size
for(var i=1; i<=128; i++){
uppercase_string.push( characters_for_upper[parseInt(Math.random() * characters_for_upper.length)]);
lowercase_string.push( characters_for_lower[parseInt(Math.random() * characters_for_lower.length)]);
numeric_string.push( characters_for_numeric[parseInt(Math.random() * characters_for_numeric.length)]);
specials_string.push( characters_for_specials[parseInt(Math.random() * characters_for_specials.length)]);
}
var counter = 0;
if (upper == true) {
counter = counter + 1;
}
if (lower == true) {
counter = counter + 1;
}
if (numeric == true) {
counter = counter + 1;
}
if (specials == true) {
counter = counter + 1;
}
var block = parseInt(length / counter);
var remainder = length % counter;
var password = [];
console.log(uppercase_string.slice(0,block).join(''));
if (upper == true) {
password.push (uppercase_string.slice(0,block).join(''));
}
if (lower == true) {
password.push(lowercase_string.slice(0,block).join(''));
}
if (numeric == true) {
password.push(numeric_string.slice(0,block).join(''));
}
if (specials == true) {
password.push(specials_string.slice(0,block).join(''));
}
if (remainder > 0) {
password.push(password.slice(0,remainder).join(''));
}
return password.join('');
}