-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
103 lines (84 loc) · 2.78 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
90
91
92
93
94
95
96
97
98
99
100
101
102
const generateBtn = document.getElementById("generate");
// render password to #password textarea
function renderPassword(password) {
let text = document.querySelector("#password");
text.value = password;
}
// generate random password according you user selections
function generatePassword(choices) {
let characterArr = [];
if (choices.lowerCase) {
characterArr.push('abcdefghijklmnopqrstuvwxyz');
}
if (choices.upperCase) {
characterArr.push('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
}
if (choices.numbers) {
characterArr.push('0123456789')
}
if (choices.symbols) {
characterArr.push('!@#$%^&*_+`-=<>?')
}
let characterStr = characterArr.join('');
let password = '';
for (let i = 0; i <= choices.passwordLength; i++) {
password = password + characterStr.charAt(Math.floor(Math.random() * characterStr.length));
}
renderPassword(password);
}
// get user input for character choices
function getInput() {
let choices = {};
let passwordLength = parseInt(prompt("Enter a password length. Password must be between 8 - 100 characters."));
if (passwordLength) {
choices.passwordLength = passwordLength;
}
if(Number.isNaN(passwordLength)){
alert("that is not a number");
return;
}
if (passwordLength < 8 ) {
alert("you must have a minimum length of 8");
return;
}
if (passwordLength > 100 ){
alert("you can have a maximum length of 100");
return;
}
let lowerCase = confirm("Would you like to include lowercase letters? Ex: a, b, c, ...");
if (lowerCase) {
choices.lowerCase = true;
} else {
choices.lowerCase = false;
}
let upperCase = confirm("Would you like to include uppercase letters? Ex: A, B, C, ...");
if (upperCase) {
choices.upperCase = true;
} else {
choices.upperCase = false;
}
let numbers = confirm("Would you like to include numbers? Ex: 1, 2, 3, ...");
if (numbers) {
choices.numbers = true;
} else {
choices.numbers = false;
}
let symbols = confirm("Would you like to include symbols? Ex: #, $, *, ...");
if (symbols) {
choices.symbols = true;
} else {
choices.symbols = false;
}
if (
lowerCase === false &&
upperCase === false &&
numbers === false &&
symbols === false
) {
alert("You must choose at least one type of character to include in password.");
return;
}
generatePassword(choices);
}
// add event listener to generate button
generateBtn.addEventListener("click", getInput);