-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (52 loc) · 2.17 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
const characters = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?",
"/"];
let passwordsEL = document.querySelectorAll('.passwords')
let lengthEl = document.querySelector('#length-inp')
let numberEl = document.querySelector('#numbers-check')
let symbolEl = document.querySelector('#symbols-check')
function generate(){
let passLength = lengthEl.value
let firstPass= createPassword(passLength)
let secondPass= createPassword(passLength)
passwordsEL[0].textContent = firstPass
passwordsEL[1].textContent = secondPass
}
function createPassword(len){
let password = ''
for(let i=0;i < len;i++){
//Get a random character from the array
let character = ''
if ( numberEl.checked && symbolEl.checked ){
let randomNumber = Math.floor( Math.random() * characters.length )
character = characters[randomNumber]
}
else if (numberEl.checked){
let randomNumber = Math.floor( Math.random() * 52 + 10 )
character = characters[randomNumber]
}
else if (symbolEl.checked){
let arr = characters.slice(0,52);
arr = arr.concat(characters.slice(52+10))
let randomNumber = Math.floor( Math.random() * arr.length )
character = arr[randomNumber]
}
else {
let randomNumber = Math.floor( Math.random() * 52 )
character = characters[randomNumber]
}
//Add it to the password string
password += character
}
return password
}
//ClipBoard Copying
passwordsEL.forEach(
function(el,i){
el.addEventListener('click',copyToClip)
}
)
function copyToClip(){
let clip = this.textContent
navigator.clipboard.writeText(clip);
alert('copied')
}