-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (91 loc) · 4.07 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
// Assignment Code
//We declare a gobal variable
var generateBtn = document.querySelector("#generate");
//We define the function to return integer in and create a new local variable
function randomInt(min, max) {
if (!max) {
max = min;
min = 0;
}
var any = Math.random();
return Math.floor(min * (1 - any) + any * max);
}
//We create a function to get the random position from the list
function pickRandom(CharacterData) {
return CharacterData[randomInt(0, CharacterData.length)];
}
//From the criteria : THEN I click the button to generate a password
//THEN I am presented with a series of prompts for password criteria
//Initially, console returns error that generatePassword is not defined, so we need to create the function
function generatePassword() {
//We use the window object to prompt for the length of the password
var Info = window.prompt("How many characters do you want your password to have?"); //It returns a string, but we need it to be a number, so we need the parseInt() function that turns string to a number
var numOfCharacters = parseInt(Info);
//The global NaN property is a value that is Not-A-Number
if (isNaN(numOfCharacters)) {
window.alert("Use only numbers please.");
//We stop the function
return;
}
//We use a statement with a conditional if the number of characters is less than 8, there will be an alert
if (numOfCharacters < 8 || numOfCharacters > 128) {
window.alert("Password must contain from 8 up to 128 characters");
//End of function
return;
}
//Criteria: WHEN asked for character types to include in the password
//THEN I confirm whether or not to include lowercase,
//User wants lowercase
var lowerCase = window.confirm("Do you want to use lower case?");
// User wants uppercase,
var upperCase = window.confirm("Do you want to use uppercase ?");
//User wants numeric, and/or
var numeric = window.confirm("Do you want to use numbers?");
//User wants special characters
var specialCharacters = window.confirm("Do you want to use special characters?");
//Criteria :WHEN I answer each prompt THEN my input should be validated
//and at least one character type should be selected
//After declaring the variables, we put them in an array and weclare a new variable to store the data
var lowerCaseData = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m"];
var upperCaseData = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"];
var numericData = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var specialCharactersData = ["!", "%", "ñ", "$", "&", "@", "*", "+", "Ç", "#"];
//var with the options the user selected, it has to be an empty array
var optionsSelected = [];
//Conditionals that pushes the data
if (lowerCase === true) {
optionsSelected.push(lowerCaseData);
}
if (upperCase === true) {
optionsSelected.push(upperCaseData);
}
if (numeric === true) {
optionsSelected.push(numericData);
}
if (specialCharacters === true) {
optionsSelected.push(specialCharactersData);
}
//Function to stop the prompts if user selects NO (Cancel) to everything
if (!lowerCase && !upperCase && !numeric && !specialCharacters) {
window.alert("Please select at least one.");
return
}
//We need a variable that will store the string with the password with the added random character
var password = "";
for (var i = 0; i < numOfCharacters; i++) {
//We will pick an item in a random position
var randomCharacter = pickRandom(optionsSelected)
var pickedCharacter = pickRandom(randomCharacter)
password = password + pickedCharacter;
console.log(pickedCharacter)
}
return password
}
// 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);