-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (92 loc) · 4.15 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Define the generatePassword function seperately
function generatePassword(){
/* ________ Local Variables ________ */
// Const variable initialized to the value of the user num input
let userNumInput = prompt('Select Desired Password Length, Min: 8, Max: 128', '48');
// const variable for charsets
// const 1.
const alphabet = 'abcedfghijklmnopqrstuvwxyz';
// const 2.
const numbers = '0123456789';
// const 3.
const special = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
// const 4.
// variable initialized to the value of confirmation strings to be combined and displayed after password generation.
let yesGenerated = 'Password generated: ';
const yesLower = 'added lowercase, ';
const yesUpper = 'added UPPER, ';
const yesSpecial = 'add3d sUm $P3C!@l, ';
const yesNumber = 'added some nums, ';
// Starting Char Set initialized to the value of an empty string.
let startingChars = '';
// scrambled chars initialized to the value of an empty string
let scrambledChars = '';
/* _________________ Conditions & Processes ____________________ */
// Main Conditional
// If the user input does not meet the length requirement, we don't need to move forward.
if (isNaN(userNumInput) || userNumInput < 8 || userNumInput > 128 ) {
alert('Stop trying to break me!');
} else {
// If user input does meet requirements, Variables are Initialized to the value of user selected boolean.
let userSelectLower = confirm('Include lowercase values?');
let userSelectUpper = confirm('How bout Uppercase?');
let userSelectSpecial = confirm('Special Chars?');
const userSelectNum = confirm('Numerical Vals as Well?');
// If the user selects a charset, add it to the value of startingCharSet & add the confirmation string to the final message.
if (userSelectLower) {
startingChars += alphabet;
yesGenerated += yesLower;
}
if (userSelectUpper) {
startingChars += alphabet.toUpperCase();
yesGenerated += yesUpper;
}
if (userSelectSpecial) {
startingChars += special;
yesGenerated += yesSpecial;
}
if (userSelectNum) {
startingChars += numbers;
yesGenerated += yesNumber;
}
// At the end of the else condition, display the yesGenerated confirm
confirm(yesGenerated);
}
// Main Functionality
// 1. yates Scramble
// Spread each character in the string of combined characters, into an array.
let arrayCharacters = [...startingChars];
// variable i = length of the array of chars
let i = arrayCharacters.length;
// temp = to hold the value of a randomly selected element in the array
let tempElement;
while (i-- > 0) {
// on each iteration, select a number at random between 0 and the current value of i
let generateIndex = Math.floor(Math.random() * (i + 1));
// Then, trade the value stored in tempElement with the value of an element selcted at a random index
tempElement = arrayCharacters[generateIndex];
// Now that the random element has been stored, store the last element in the array
arrayCharacters[generateIndex] = arrayCharacters[i];
// Trade the last element in the array with the random
arrayCharacters[i] = tempElement;
}
// Outside of the loop, convert the scrambled array into a string value.
scrambledChars = arrayCharacters.join('');
// Slice the scrambledChars string beginning with 0 index, and ending at userInputNum
return scrambledChars.slice(0, userNumInput);
}
// End generatePassword
// Assignment Code
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
// 1. Define generatePassword
// 2. Return the value of generate password to the global memory
// 3.
var password = generatePassword();
var passwordText = document.querySelector("#password");
// Could I modify the value of passwordText by making it equal to the value produced by generatePassword invokation?
passwordText.value = password;
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);