-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (179 loc) · 6.08 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
(() => {
// grab the necessary DOM elements
const passwordButton = document.getElementById('button');
const outPut1 = document.getElementById('option-1');
const outPut2 = document.getElementById('option-2');
const outPut3 = document.getElementById('option-3');
const outPut4 = document.getElementById('option-4');
// const textInput = document.getElementById('text');
const rangeSlider = document.getElementById('text');
const sliderValue = document.getElementById('sliderValue');
console.log('outPut1 is', outPut1);
console.log(
'rangeSlider is',
rangeSlider,
'rangeSlider.value is',
rangeSlider.value,
'typeof rangeSlider.value is',
typeof rangeSlider.value
);
// create variables
let passwordArray1 = [];
let passwordArray2 = [];
let passwordArray3 = [];
let passwordArray4 = [];
let characterArray =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+=-~'.split(
''
);
// create functions
function initializeSlider() {
console.log('initializeSlider called');
const value = rangeSlider.value || '';
// since the left position of the thumb changes according to percentage, we can use the percentage to decide where the slider will be
const min = parseInt(rangeSlider.min, 10);
const max = parseInt(rangeSlider.max, 10);
const percentage = ((value - min) / (value - max)) * 100;
sliderValue.innerText = value;
// calculate left position
let originalBoundaries = rangeSlider.getBoundingClientRect();
let originalWidth = originalBoundaries.width;
let originalLeft = originalBoundaries.left;
let numAfterPerc = value / 50;
sliderValue.style.left = `calc(${
originalLeft + originalWidth * numAfterPerc
}px + ${numAfterPerc * 0.5}rem)`; // Adjust formula as needed
sliderValue.style.top = `${originalBoundaries.top - 50}px`;
}
function generateRandomString() {
console.log('generateRandomString called');
let num = getNumber();
validateNumber(num);
var x = checkWhichSlotsAreEmpty();
for (let i = 0; i < num; i++) {
var y = generateRandomNumber();
updateArray(x, y);
}
console.log('x going into displayArray is', x);
displayArray(x);
}
function getNumber() {
console.log('getNumber called');
let newNumber = document.getElementById('text').value;
console.log('newNumber is', newNumber);
return newNumber === '' ? 8 : parseInt(newNumber, 10);
}
function validateNumber(newNumber) {
console.log('validateNumber called: newNumber is', newNumber);
if (!newNumber) {
alert('please enter a non-zero number');
} else if (Math.sign(newNumber) !== 1) {
alert('please enter a positive number');
} else {
return newNumber;
}
}
function updateArray(chosenarray, value) {
console.log(
'updateArray called. chosenarray input parameter is',
chosenarray,
'value is',
value
);
chosenarray.push(characterArray[value]);
console.log('chosenarray was updated to', chosenarray);
}
function generateRandomNumber() {
console.log('generateRandomNumber called');
return Math.floor(Math.random() * characterArray.length);
}
function checkWhichSlotsAreEmpty() {
console.log('checkWhichSlotsAreEmpty called');
if (passwordArray1.length === 0) {
return passwordArray1;
} else if (passwordArray2.length === 0) {
return passwordArray2;
} else if (passwordArray3.length === 0) {
return passwordArray3;
} else if (passwordArray4.length === 0) {
passwordButton.innerText = 'Reset';
return passwordArray4;
} else {
reset();
}
}
function findTarget(evt) {
console.log('findTarget fired');
let targetElement;
if (evt.target.className === 'option') {
targetId = evt.target.id;
targetElement = document.getElementById(targetId);
if (evt.target.innerHTML) {
navigator.clipboard.writeText(evt.target.value);
alert('Password successfully copied!');
} else {
alert('no password to copy');
}
}
}
function turnTextWhite(element) {
element.style.color = 'white';
}
function turnTextBlack(...elements) {
elements.forEach((el) => {
el.style.color = '#ffffff40';
});
}
function displayArray(outPutArray) {
console.log('displayArray called');
let outPutRegex = outPutArray.toString().replace(/,/g, '');
console.log('outPutRegex is', outPutRegex);
if (outPut1.innerText === '' || outPut1.innerText === 'password option 1') {
console.log('outPut1 is empty');
outPut1.innerText = outPutRegex;
turnTextWhite(outPut1);
} else if (
outPut2.innerText === '' ||
outPut2.innerText === 'password option 2'
) {
console.log('outPut2 is empty');
outPut2.innerText = outPutRegex;
turnTextWhite(outPut2);
} else if (
outPut3.innerText === '' ||
outPut3.innerText === 'password option 3'
) {
console.log(' outPut3 is empty');
outPut3.innerText = outPutRegex;
turnTextWhite(outPut3);
} else if (
outPut4.innerText === '' ||
outPut4.innerText === 'password option 4'
) {
console.log('outPut4 is empty');
outPut4.innerText = outPutRegex;
turnTextWhite(outPut4);
}
}
function reset() {
console.log('reset called');
passwordArray1 = [];
passwordArray2 = [];
passwordArray3 = [];
passwordArray4 = [];
outPut1.innerText = 'password option 1';
outPut2.innerText = 'password option 2';
outPut3.innerText = 'password option 3';
outPut4.innerText = 'password option 4';
turnTextBlack(outPut1, outPut2, outPut3, outPut4);
rangeSlider.value = '';
passwordButton.innerText = 'Generate Password';
}
// add listeners
passwordButton.addEventListener('click', generateRandomString);
// adjustments to turn text field into range finder
document.addEventListener('click', findTarget);
document.addEventListener('DOMContentLoaded', initializeSlider);
window.addEventListener('resize', initializeSlider);
rangeSlider.addEventListener('input', initializeSlider);
})();