diff --git a/projects/Password Generator/index.html b/projects/Password Generator/index.html index 6b9d20d31..c86f2a33d 100644 --- a/projects/Password Generator/index.html +++ b/projects/Password Generator/index.html @@ -12,8 +12,11 @@

Click to generate a random password!

+ +
+
diff --git a/projects/Password Generator/script.js b/projects/Password Generator/script.js index 0486e8dd7..61ee2c472 100644 --- a/projects/Password Generator/script.js +++ b/projects/Password Generator/script.js @@ -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'; @@ -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]; @@ -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(); -}) \ No newline at end of file +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; +}