Skip to content

Commit

Permalink
Your detailed commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
nilesh-fatfatwale committed Oct 2, 2024
1 parent 244ce1e commit 3ea84ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions projects/Password Generator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
<main>
<h1>Click to generate a random password!</h1>
<input type="text" id="password">
<input type="range" id="passwordLength" min="1" max="50" value="8" oninput="updateLengthDisplay()">
<label for="passwordLength">Password length: <span id="lengthDisplay">8</span></label>
<div>
<button id="create">Create</button>
<button id="copy">Copy</button>
<button id="clear">Clear</button>
</div>
</main>
Expand Down
41 changes: 23 additions & 18 deletions projects/Password Generator/script.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const field = document.querySelector('#password');
const create = document.querySelector('#create');
const clear = document.querySelector('#clear');

const copy = document.querySelector('#copy');
const passwordLength = document.querySelector('#passwordLength');
const lengthDisplay = document.querySelector('#lengthDisplay');

const uppercase = 'QWERTYUIOPASDFGHJKLZXCVBNM';
const numbers = '1234567890';
Expand All @@ -12,7 +14,7 @@ String.prototype.shuffle = function () {
var a = this.split(""),
n = a.length;

for(var i = n - 1; i > 0; i--) {
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
Expand All @@ -21,27 +23,30 @@ String.prototype.shuffle = function () {
return a.join("");
}

function randomPassword(){
function randomPassword(length) {
let characters = uppercase + lowercase + numbers + symbols;
let password = '';
for (let i = 0; i < 3; i++){
password += uppercase.charAt(Math.floor(Math.random() * uppercase.length))
}
for (let i = 0; i < 3; i++){
password += lowercase.charAt(Math.floor(Math.random() * lowercase.length))
}
for (let i = 0; i < 2; i++){
password += numbers.charAt(Math.floor(Math.random() * numbers.length))
}
for (let i = 0; i < 2; i++){
password += symbols.charAt(Math.floor(Math.random() * symbols.length))

for (let i = 0; i < length; i++) {
password += characters.charAt(Math.floor(Math.random() * characters.length));
}

return password.shuffle();
}

clear.addEventListener('click', ()=>{
clear.addEventListener('click', () => {
field.value = '';
})

create.addEventListener('click', ()=>{
field.value = randomPassword();
})
create.addEventListener('click', () => {
const length = parseInt(passwordLength.value);
field.value = randomPassword(length);
})

copy.addEventListener('click', () => {
navigator.clipboard.writeText(field.value);
})

function updateLengthDisplay() {
lengthDisplay.textContent = passwordLength.value;
}

0 comments on commit 3ea84ad

Please sign in to comment.