-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
136 lines (99 loc) · 3.72 KB
/
app.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const topContainer = document.querySelector('.top-container');
const bottomBtn = document.querySelector('.bottom-btn');
const submitBtn = document.querySelector('.submit-btn');
const homeBtn = document.querySelector('.home-btn');
const questionModal = document.querySelector('.question-modal');
const answerModal = document.querySelector('.answer-modal');
questionModal.style.display = 'none';
answerModal.style.display = 'none';
homeBtn.style.display = 'none';
bottomBtn.addEventListener('click', () => {
topContainer.style.display = 'none';
questionModal.style.display = 'block';
bottomBtn.style.display = 'none';
homeBtn.style.display = 'block';
})
submitBtn.addEventListener('click', () => {
topContainer.style.display = 'none';
questionModal.style.display = 'none';
bottomBtn.style.display = 'none';
answerModal.style.display = 'block';
})
homeBtn.addEventListener('click', () => {
location.reload();
})
//Communicating with the DOM
var answerEl = document.getElementById("myInput");
var lengthEl = document.getElementById("length");
var numberEl = document.getElementById("number");
var lowerEl = document.getElementById("lower");
var upperEl = document.getElementById("upper");
var symbolEl = document.getElementById("symbol");
// var copyEl = document.getElementById("copy");
var generateEl = document.getElementById("generate");
const randomFunc = {
upper: getRandomUpperCase,
lower: getRandomLowerCase,
number: getRandomNumber,
symbol: getRandomSymbol
};
//generate event
generateEl.addEventListener('click', () => {
//const length = parseInt(lengthEl.value);
const length = +lengthEl.value;
const hasUpper = upperEl.checked;
const hasLower = lowerEl.checked;
const hasNumber = numberEl.checked;
const hasSymbol = symbolEl.checked;
answerEl.value = generatePassword(hasUpper, hasLower, hasNumber, hasSymbol, length);
});
//Generate Password Function
function generatePassword(upper, lower, number, symbol, length) {
let generatedPassword = "";
const typesCount = upper + lower + number + symbol;
//console.log(typesCount);
const typesArr = [{ upper }, { lower }, { number }, { symbol }].filter(item => Object.values(item)[0]);
if (typesCount === 0) {
return '';
}
for (let i = 0; i < length; i += typesCount) {
typesArr.forEach(type => {
const funcName = Object.keys(type)[0];
generatedPassword += randomFunc[funcName]();
});
}
const finalPassword = generatedPassword.slice(0, length);
return finalPassword;
}
// Generating random values
function getRandomUpperCase() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
function getRandomLowerCase() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
function getRandomNumber() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
function getRandomSymbol() {
var symbol = "!@#$%^&*(){}[]=<>/,.|~?";
return symbol[Math.floor(Math.random() * symbol.length)];
}
console.log(getRandomSymbol());
let me = document.getElementById("length").value;
var btn = document.getElementById("generate");
generate.addEventListener('click', () => {
console.log(me);
})
// xjnjdjd
function myFunction() {
/* Get the text field */
var copyText = document.getElementById("myInput");
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied to Clipboard");
}