-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (61 loc) · 2.21 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
// variable statements
var possibleCharacters = "";
var numbers = "0123456789";
var lower = "abcdefghijklmnopqrstuvwxyz";
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var special = "~`!@#$%^&*()-_=+{}[]|/?.<>,";
var finalPass = "";
//Add Event Listener to generate button
var generateBtn = document.querySelector("#generate");
//Write password to the #password input
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
generateBtn.addEventListener("click", writePassword);
//main password function
function generatePassword() {
var passwordLength = prompt(
"How many characters would you like your password to have? The minimum number of characters is 8, and the maximum is 128."
);
//Prompt for password length
if (passwordLength < 8 || passwordLength > 128) {
alert("You must choose a number between 8 and 128.");
return;
}
// Confirm Character type
var confirmLower = confirm("Would you like to include lower case letters?");
var confirmUpper = confirm("Would you like to include upper case letters?");
var confirmNum = confirm("Would you like to include numbers?");
var confirmSpecial = confirm("Would you like to include special characters?");
//If true, alert user to selected character types
if (confirmNum) {
possibleCharacters += numbers;
console.log(possibleCharacters);
alert("You have chosen to include numbers.");
}
if (confirmLower) {
possibleCharacters += lower;
console.log(possibleCharacters);
alert("You have chosen to include lower case letters.");
}
if (confirmUpper) {
possibleCharacters += upper;
console.log(possibleCharacters);
alert("You have chosen to include upper case letters.");
}
if (confirmSpecial) {
possibleCharacters += special;
console.log(possibleCharacters);
alert("You have chosen to include special characters.");
}
//For-Loop and equation
for (var i = 0; i < passwordLength; i++) {
var randomIndex = Math.floor(Math.random() * possibleCharacters.length);
var randomElement = possibleCharacters[randomIndex];
finalPass = finalPass + randomElement;
}
console.log(finalPass);
return finalPass;
}