-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
38 lines (31 loc) · 952 Bytes
/
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
const generatePassword = length => {
document.querySelector("#password").innerHTML = createPassword(length);
}
const createPassword = length => {
const lowercase = "abcdefghijklmnopqrstuvwxyz";
const uppercase = lowercase.toUpperCase();
const numbers = "0123456789";
const [lower, upper, num] = [
Math.ceil(length/3),
Math.ceil(length/3),
length - 2 * Math.ceil(length/3),
]
const passwordCharacters = shuffle([
...getCharacters(lower, lowercase),
...getCharacters(upper, uppercase),
...getCharacters(num, numbers)
]);
const password = passwordCharacters.join("");
return password;
}
const getCharacters = (length, string) => {
let characters = [];
for(let i = 0; i < length; i++) {
let random = Math.floor(Math.random() * string.length);
characters[i] = string.charAt(random);
}
return characters;
}
const shuffle = array => {
return array.sort(() => Math.random() - 0.5);
}