-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
68 lines (51 loc) · 1.65 KB
/
main.js
File metadata and controls
68 lines (51 loc) · 1.65 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
import { getRandomChar, getRandomInt, replaceAt } from "./utils.js";
const VOWELS = ["a", "e", "i", "o", "u", "y"]
const CONSONANTS = ["b","c","d","f","g","h","k","m","n","p","q","r","s","t","v","w","x","z"]
const SET = {
uppercase: "ABCDEFGHJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
}
const getValue = (inputValue, defaultValue) => {
const preformed = Math.abs(inputValue);
if (isNaN(preformed) || preformed > 128 || preformed === 0) {
return defaultValue;
}
return inputValue;
}
const generate = () => {
const result = document.querySelector("#result");
const length = document.querySelector("#inputLength").value;
const count = document.querySelector("#inputCount").value;
const passwords = getPasswords(getValue(length, 20), getValue(count, 1));
result.innerHTML = passwords
.map(item =>
`<div class="password">
<span>${item}</span>
</div>`)
.join("")
}
const getPasswords = (length, count) => {
const passwords = [];
for (let i = 0; i < count; i++) {
passwords.push(getPassword(length))
}
return passwords
}
const getPassword = (length) => {
const uppercase = getRandomChar(SET.uppercase)
const number = getRandomChar(SET.numbers)
const lowercase = [];
for (let i = 0; lowercase.length < length; i++) {
lowercase.push(getRandomChar(CONSONANTS))
lowercase.push(getRandomChar(VOWELS))
}
let result = lowercase.join("")
result = replaceAt(result, uppercase, getRandomInt(length))
result = replaceAt(result, number, getRandomInt(length))
return result
}
const init = () => {
document.querySelector("#btnGenerate").addEventListener('click', generate);
generate();
}
init();