Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
149 lines (131 loc) · 5.73 KB
/
Copy pathscript.js
File metadata and controls
149 lines (131 loc) · 5.73 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Start working code:
// N.B. In Javascript, an array is a list of elements may it be string, numbers, Boolean, etc.
// Concatenation: The word concatenate is just another way of saying "to combine" or "to join together". The CONCATENATE function allows you to combine text from different cells into one cell. Concatination is adding two arrays or more than 2 arrays. Similar to add, array1+ array2, concat () method works similarly in javascript. It appends the array elements at the end of the array to which the user wants to combine. (Source: www.educba.com/javascript-array-concat/)
// The Password Generator utilises the concatenation function within javascript
// User input variables:
var enter;
var confirmNumber;
var confirmCharacter;
var confirmUppercase;
var confirmLowercase;
// Start with setting Password variable values:
// Special characters
character = ["!", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "\:", "\;", " < ", "=", " > ", " ? ", "@", "[", "\\", "]", " ^ ", "_", "`", "{", "|", "}", "~"];
// Numeric characters
number = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// Alphabetical characters
alpha = ["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"];
// Space is for the Uppercase conversion
space = [];
// Choices declared outside the if statement so they can be concatenated upon condition
var choices;
// converts letters to uppercase
var toUpper = function (x) {
return x.toUpperCase();
};
// creates a variable for uppercase conversion
alpha2 = alpha.map(toUpper);
var get = document.querySelector("#generate");
get.addEventListener("click", function () {
ps = generatePassword();
document.getElementById("password").placeholder = ps;
});
// Start function to generate password using string
function generatePassword() {
// Asks for user input
enter = parseInt(prompt("What is the length of your password? Choose between 8 and 128"));
// First if statement for user validation (using logial comparison operators || = or)
if (!enter) {
alert("Please input a value");
} else if (enter < 8 || enter > 128) {
// Validates user input
// Start user input prompts
enter = parseInt(prompt("Please choose a number between 8 and 128 (inclusive)"));
} else {
// Continues once user input is validated
confirmNumber = confirm("Do you want the password to contain numbers?");
confirmCharacter = confirm("Do you want the password to contain special characters?");
confirmUppercase = confirm("Do you want the password to contain Uppercase letters?");
confirmLowercase = confirm("Do you want the password to contain Lowercase letters?");
};
// Else if for 4 negative options
if (!confirmCharacter && !confirmNumber && !confirmUppercase && !confirmLowercase) {
choices = alert("Please choose a criteria in order to continue");
}
// First if statement that uses user input prompts to determine choices
// Else if for 4 positive options
else if (confirmCharacter && confirmNumber && confirmUppercase && confirmLowercase) {
choices = character.concat(number, alpha, alpha2);
}
// Else if for 3 positive options
else if (confirmCharacter && confirmNumber && confirmUppercase) {
choices = character.concat(number, alpha2);
}
else if (confirmCharacter && confirmNumber && confirmLowercase) {
choices = character.concat(number, alpha);
}
else if (confirmCharacter && confirmLowercase && confirmUppercase) {
choices = character.concat(alpha, alpha2);
}
else if (confirmNumber && confirmLowercase && confirmUppercase) {
choices = number.concat(alpha, alpha2);
}
// Else if for 2 positive options
else if (confirmCharacter && confirmNumber) {
choices = character.concat(number);
} else if (confirmCharacter && confirmLowercase) {
choices = character.concat(alpha);
} else if (confirmCharacter && confirmUppercase) {
choices = character.concat(alpha2);
}
else if (confirmLowercase && confirmNumber) {
choices = alpha.concat(number);
} else if (confirmLowercase && confirmUppercase) {
choices = alpha.concat(alpha2);
} else if (confirmNumber && confirmUppercase) {
choices = number.concat(alpha2);
}
// Else if for 1 positive option
else if (confirmCharacter) {
choices = character;
}
else if (confirmNumber) {
choices = number;
}
else if (confirmLowercase) {
choices = alpha;
}
// Created space variable to fill uppercase conversion
else if (confirmUppercase) {
choices = space.concat(alpha2);
};
// password variable is an array placeholder for user generated amount of length
var password = [];
// Start random selection variables:
// Random selection for all variables:
for (var i = 0; i < enter; i++) {
var pickChoices = choices[Math.floor(Math.random() * choices.length)];
password.push(pickChoices);
}
// This joins the password array and converts it to a string
var ps = password.join("");
UserInput(ps);
return ps;
}
// This puts the password value into the textbox
// Changed function input to use textcontent
function UserInput(ps) {
document.getElementById("password").textContent = ps;
}
var copy = document.querySelector("#copy");
copy.addEventListener("click", function () {
copyPassword();
});
// This copies the password value to clipboard
// Code example demonstrated in a youtube video:
// Source: https://youtu.be/9sT03jEwcaw
function copyPassword() {
document.getElementById("password").select();
document.execCommand("Copy");
alert("Password has been copied to clipboard!");
}