-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
83 lines (67 loc) · 2.03 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
// Assignment Code
var generateBtn = document.querySelector("#generate");
var resultEl = document.getElementById('password');
var pwLength;
var pwUppers;
var pwNumber;
var pwSymbols;
var pwLowers;
var ranArr = [];
// Write password to the #password input
function writePassword() {
generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = ranArr.join('');
}
//Prompts user for input
function getPrompt() {
pwLength = prompt("How long do you want the length of or password? (Min: 8. Max: 120.)");
pwUppers = confirm("Do you want uppercase letters?");
pwLowers = confirm("Do you want lowers? y or n");
pwNumber = confirm("Do you want Numbers? y or n");
pwSymbols = confirm("Do you want Symbols? y or n");
}
function generatePassword() {
getPrompt();
var typeArr = [];
//Type check that pushes functions into an arr
console.log("Inside generate password." + +pwUppers);
for(var j = 0; j < pwLength; j++) {
if (pwUppers) {
typeArr.push(getRandomUpper());
console.log(typeArr);
}
if (pwLowers) {
typeArr.push(getRandomLower());
}
if (pwSymbols) {
typeArr.push(getRandomSymbol());
}
if (pwNumber) {
typeArr.push(getRandomNumber());
}
}
console.log(typeArr);
//Loop that pushes randomly generated char to ranArr
for(var i = 0; i < pwLength; i++){
ranArr.push(typeArr[Math.floor(Math.random() * typeArr.length)]);
console.log(ranArr);
}
}
//Generator functions for each output type
function getRandomLower() {
return String.fromCharCode(Math.floor((Math.random()* 26) + 97));
};
function getRandomUpper() {
return String.fromCharCode(Math.floor((Math.random()* 26) + 65));
}
function getRandomNumber() {
return String.fromCharCode(Math.floor((Math.random()* 10) + 48));
}
function getRandomSymbol() {
const symbols = '!@#$%^&*(){}[]=<>/,.`~';
return symbols[Math.floor(Math.random() * symbols.length)];
}
//console.log(getRandomSymbol());
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);