-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
89 lines (72 loc) · 2.85 KB
/
script.js
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
// Assignment code here
function generatePassword() {
// variables to string character choices
var lowerCase = "abcedfghijklmnopqrstuvwxyz";
var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numberalCharacters = "0123456789";
var specialCharacters = "$!#'%()*+&,-./:;<=>?@[^]_`{|}~";
var possibleCharacters = "";
var generatedPassword = "";
var count = 0;
// variable to determine password count
var passwordLength = parseInt (prompt ("How many characters would you like?"));
console.log("passwordLength" , passwordLength);
if (isNaN(passwordLength) == true) {
alert ("Password length must be provided as a number");
return ("Invalid input. Password not generated.");
}
if (passwordLength < 12 || passwordLength > 128 ) {
alert ("Password must be at least 12 to 128 characters.");
return ("Password not generated");
}
count = passwordLength;
// bolleon variables for the character options
var hasLC = confirm ("Do you want to include LowerCase letters?");
var hasUC = confirm ("Do you want to include UpperCase letters?");
var hasNC = confirm ("Do you want to include numbers?");
var hasSP = confirm ("Do you want to include symbols?");
// prompt to request at least selection for password generator
if (hasLC === false && hasUC === false && hasNC === false && hasSP === false) {
alert ("At least one character type must be selected");
return ("Select which characters to include.");
}
if (hasLC === true) {
possibleCharacters+= lowerCase;
// generatedPassword+= lowerCase [Math.floor (Math.random() * lowerCase.length)];
// console.log("Generated password" , generatedPassword);
// count--;
}
if (hasUC === true) {
possibleCharacters+= upperCase;
// generatedPassword+= upperCase [Math.floor (Math.random() * upperCase.length)];
// console.log ("Generated password" , generatedPassword);
// count--;
}
if (hasNC === true) {
possibleCharacters+= numberalCharacters;
// generatedPassword+= numberalCharacters [Math.floor (Math.random() * numberalCharacters.length)];
// console.log ("generated password" , generatedPassword);
// count--;
}
if (hasSP === true) {
possibleCharacters+= specialCharacters;
// generatedPassword+= specialCharacters [Math.floor (Math.random() * specialCharacters.length)];
// console.log ("generated password" , generatedPassword);
// count--;
}
for (var i = 0; i < count; i++) {
generatedPassword+=possibleCharacters[Math.floor (Math.random() * possibleCharacters.length)];
}
console.log(generatedPassword);
return generatedPassword;
}
// Get references to the #generate element
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);