- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathscript.js
More file actions
Latest commit
59 lines (52 loc) · 2.26 KB
/
Copy pathscript.js
File metadata and controls
59 lines (52 loc) · 2.26 KB
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
constcharacterAmountRange=document.getElementById('characterAmountRange')
constcharacterAmountNumber=document.getElementById('characterAmountNumber')
constincludeUppercaseElement=document.getElementById('includeUppercase')
constincludeNumbersElement=document.getElementById('includeNumbers')
constincludeSymbolsElement=document.getElementById('includeSymbols')
constform=document.getElementById('passwordGeneratorForm')
constpasswordDisplay=document.getElementById('passwordDisplay')
constUPPERCASE_CHAR_CODES=arrayFromLowToHigh(65,90)
constLOWERCASE_CHAR_CODES=arrayFromLowToHigh(97,122)
constNUMBER_CHAR_CODES=arrayFromLowToHigh(48,57)
constSYMBOL_CHAR_CODES=arrayFromLowToHigh(33,47).concat(
arrayFromLowToHigh(58,64)
).concat(
arrayFromLowToHigh(91,96)
).concat(
arrayFromLowToHigh(123,126)
)
characterAmountNumber.addEventListener('input',syncCharacterAmount)
characterAmountRange.addEventListener('input',syncCharacterAmount)
form.addEventListener('submit',e=>{
e.preventDefault()
constcharacterAmount=characterAmountNumber.value
constincludeUppercase=includeUppercaseElement.checked
constincludeNumbers=includeNumbersElement.checked
constincludeSymbols=includeSymbolsElement.checked
constpassword=generatePassword(characterAmount,includeUppercase,includeNumbers,includeSymbols)
passwordDisplay.innerText=password
})
functiongeneratePassword(characterAmount,includeUppercase,includeNumbers,includeSymbols){
letcharCodes=LOWERCASE_CHAR_CODES
if(includeUppercase)charCodes=charCodes.concat(UPPERCASE_CHAR_CODES)
if(includeSymbols)charCodes=charCodes.concat(SYMBOL_CHAR_CODES)
if(includeNumbers)charCodes=charCodes.concat(NUMBER_CHAR_CODES)
constpasswordCharacters=[]
for(leti=0;i<characterAmount;i++){
constcharacterCode=charCodes[Math.floor(Math.random()*charCodes.length)]
passwordCharacters.push(String.fromCharCode(characterCode))
}
returnpasswordCharacters.join('')
}
functionarrayFromLowToHigh(low,high){
constarray=[]
for(leti=low;i<=high;i++){
array.push(i)
}
returnarray
}
functionsyncCharacterAmount(e){
constvalue=e.target.value
characterAmountNumber.value=value
characterAmountRange.value=value
}