Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
104 lines (75 loc) · 2.96 KB
/
Copy pathscript.js
File metadata and controls
104 lines (75 loc) · 2.96 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var passwordLength = document.getElementById("password-length");
var upperCase = document.querySelector("#uppercase");
var lowerCase = document.querySelector("#lowercase");
var specialChar = document.querySelector("#special");
var numeric = document.querySelector("#numeric");
var submit = document.querySelector("#submit-button")
var printPassword = document.getElementById("print-password")
var clear = document.getElementById("clear");
var alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
var lowerCaseArray = alphabet.split(" ");
var numericArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var specialChars = "! @ # $ % ^ & * ( )"
var specialCharArray = specialChars.split(" ");
var penultimateArray = [];
var finalArray = [];
function shuffle(array) {
array.sort(() => Math.random() - 0.5);
}
function pickAmount(array) {
for (let i = 0; i < passwordLength.value; i++) {
var randomNumber = Math.floor(Math.random() * array.length);
finalArray.push(array[randomNumber]);
}
}
function clearFunction() {
window.location.reload();
}
function checkPasswordLength() {
event.preventDefault();
console.log(passwordLength.value);
if (passwordLength.value == 0) {
alert("Must pick a password length.")
printPassword = "";
}
else if (passwordLength.value < 8) {
alert("Password must be at least 8 characters long.");
printPassword = "";
}
else if (passwordLength.value > 128) {
alert("Password must be no more the 128 characters long.")
printPassword = "";
}
};
function mustCheckOne() {
if (upperCase.checked == false && lowerCase.checked == false && specialChar.checked == false && numeric.checked == false) {
alert("Must pick at least one character-type.");
}
};
function pickFromArray() {
for (let i = 0; i < 128; i++) {
if (lowerCase.checked == true) {
var randomNumber = Math.floor(Math.random() * lowerCaseArray.length);
penultimateArray.push(lowerCaseArray[randomNumber]);
};
if (upperCase.checked == true) {
var randomNumber = Math.floor(Math.random() * lowerCaseArray.length);
penultimateArray.push(lowerCaseArray[randomNumber].toUpperCase());
};
if (numeric.checked == true) {
var randomNumber = Math.floor(Math.random() * numericArray.length);
penultimateArray.push(numericArray[randomNumber]);
};
if (specialChar.checked == true) {
var randomNumber = Math.floor(Math.random() * specialCharArray.length);
penultimateArray.push(specialCharArray[randomNumber]);
};
shuffle(penultimateArray);
console.log(penultimateArray.join(''));}
pickAmount(penultimateArray);
printPassword.textContent = finalArray.join('');
};
clear.addEventListener("click", clearFunction)
submit.addEventListener("click", checkPasswordLength);
submit.addEventListener("click", mustCheckOne);
submit.addEventListener("click", pickFromArray);