Passed
Push — main ( e44d9d...846cab )
by LCS
04:31
created

password.js ➔ generatePassword   B

Complexity

Conditions 7

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
dl 0
loc 20
rs 8
c 0
b 0
f 0
1
const lengthInput = document.getElementById("length");
2
const lowercaseCb = document.getElementById("lowercase");
3
const uppercaseCb = document.getElementById("uppercase");
4
const numbersCb = document.getElementById("numbers");
5
const symbolsCb = document.getElementById("symbols");
6
const resultField = document.getElementById("result");
7
const generateBtn = document.getElementById("generateBtn");
8
const copyBtn = document.getElementById("copyBtn");
9
10
const lowercase = "abcdefghijklmnopqrstuvwxyz";
11
const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
12
const numbers = "0123456789";
13
const symbols = "!@#$%^&*()_+{}[]<>?/|";
14
15
function generatePassword() {
16
  let chars = "";
17
  if (lowercaseCb.checked) chars += lowercase;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
18
  if (uppercaseCb.checked) chars += uppercase;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
19
  if (numbersCb.checked) chars += numbers;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
  if (symbolsCb.checked) chars += symbols;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
21
22
  const length = parseInt(lengthInput.value);
23
  if (!chars) {
24
    resultField.value = "Select at least one option!";
25
    return;
26
  }
27
28
  let password = "";
29
  for (let i = 0; i < length; i++) {
30
    const rand = Math.floor(Math.random() * chars.length);
31
    password += chars[rand];
32
  }
33
  resultField.value = password;
34
}
35
36
generateBtn.addEventListener("click", generatePassword);
37
38
copyBtn.addEventListener("click", () => {
39
  if (resultField.value) {
40
    navigator.clipboard.writeText(resultField.value);
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
41
    alert("Password copied!");
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
42
  }
43
});