-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
77 lines (59 loc) · 2.94 KB
/
script.js
File metadata and controls
77 lines (59 loc) · 2.94 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
function app() {
// Assignment code here
var answer = window.confirm("Do you want to create a password? Please click okay to begin.");
if (answer){
// Prompts user to enter a password length
var passwordLength = prompt("Enter the length of password at least 8 characters but no more than 128 characters. ")
}
//Converts the entered value to a number
var lengthNumber = parseInt(passwordLength);
//Prompts user to enter lowercase, uppercase, and/or special characters
var includeLowerCase = confirm("Include lowercase letters? Click OK for yes, or CANCEL for no.");
var includeUpperCase = confirm("Include uppercase letters? Click OK for yes, or CANCEL for no.");
var includeNumbers = confirm("Include numbers? Click OK for yes, or CANCEL for no");
var includeSpecialCharacters = confirm("Include special characters? Click OK for yes, or CANCEL for no");
var upperCaseLetters='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var lowerCaseLetters='abcdefghijklmnopqrstuvwxyz';
var numbers='0123456789';
var specialCharacters = '!@#$*&';
//empty array which can include all the uppercase, lowercase, numbers, and special character combinations
var potentialCharacters = '';
// Add uppercase letters if selected
if (includeUpperCase) {
potentialCharacters = potentialCharacters.concat(upperCaseLetters);
}
// Add lowercase letters if selected
if (includeLowerCase) {
potentialCharacters = potentialCharacters.concat(lowerCaseLetters);
}
// Add numbers if selected
if (includeNumbers) {
potentialCharacters = potentialCharacters.concat(numbers);
}
// Add special characters if selected
if (includeSpecialCharacters) {
potentialCharacters = potentialCharacters.concat(specialCharacters);
}
// Write password to the #password input
function generatePassword(length,characters) {
//console.log("Hit generatePassword")
length = length || 128;
characters = characters || "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxya0123456789";
var password = '';
for (var i = 0; i < length; i++){
var randomIndex = Math.floor(Math.random() * potentialCharacters.length);
password += potentialCharacters.charAt(randomIndex);
}
return password;
} // generatePassword
//variables for password length, characters such as uppercase, lowercase, numbers, and special characters
var passwordCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxya0123456789!@#$*&";
var password = generatePassword(passwordLength, passwordCharacters);
//element ID with "password"
var passwordText = document.querySelector("#password");
passwordText.value = password;
} // app
// Get references to the #generate element
var generateBtn = document.querySelector("#generate");
// Add event listener to generate button
generateBtn.addEventListener("click", app)