Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (59 loc) · 2.61 KB
/
Copy pathscript.js
File metadata and controls
75 lines (59 loc) · 2.61 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
// Assignment Code here
// Password parameters
var numbers = [1,2,3,4,5,6,7,8,9,0];
var specials = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")"];
var letters = Array.from(Array(26)).map( (_, i) => i + 97);
var lowCaseLetters = letters.map(code => String.fromCharCode(code));
var upCaseLetters = lowCaseLetters.map(letter => letter.toUpperCase())
// Generate function
var generatePassword = function(password, passwordArray) {
var password = "";
// Prompt user to set password length
while (true) {
var passwordLength = parseInt(prompt("Set the length of your password between 18 and 36 characters"));
// If user sets a correct length do nothing and move on to the next parameter
if (passwordLength >= 18 && passwordLength <= 36) {
break;
}
// If user sets an incorrect length
alert("Please select a number between 18 - 36");
}
// Once user has chosen a valid number, they will choose the rest of the parameters
var specials_choice = confirm("Select 'OK' if you would like to include special characters");
var numbers_choice = confirm("Select 'OK' if you would like to include numbers");
var upCaseLetters_choice = confirm("Select 'OK' if you would like to include uppercase letters");
var lowCaseLetters_choice = confirm("Select 'OK' if you would like to include lowercase letters");
// Array to concat all possible true options and pass them into the array
var passwordArray = []
if (specials_choice) {
passwordArray = passwordArray.concat(specials)
}
if (numbers_choice) {
passwordArray = passwordArray.concat(numbers)
}
if (upCaseLetters_choice) {
passwordArray = passwordArray.concat(upCaseLetters)
}
if (lowCaseLetters_choice) {
passwordArray = passwordArray.concat(lowCaseLetters)
};
// Loop to assign random characters
while (password.length < passwordLength) {
password += passwordArray[Math.floor(Math.random() * passwordArray.length)];
// validate if the user made no selections for the parameters
if (specials_choice === false && numbers_choice === false && upCaseLetters_choice === false && lowCaseLetters_choice === false) {
alert("You need to I select at least one parameter. Please try again.")
};
}
return password;
};
// Get references to the #generate element
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);