Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1db4f9

Browse files
task day 18 moderate
1 parent 2b364d0 commit f1db4f9

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

‎day18/STARTER-FILES/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@
9191
>
9292
</div>
9393
</div>
94-
<script src="/day18/app.js"></script>
94+
<script src="./../app.js"></script>
9595
</body>
9696
</html>

‎day18/app.js

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ const passwordElem = document.getElementById("password"),
99
copyButtonElem = document.querySelector(".copy"),
1010
copyButtonTextElem = document.querySelector(".copy span");
1111

12-
const newPassword = [];
12+
const checkboxes = document.querySelectorAll("input[type=checkbox]");
1313

14-
function showSymbol() {
15-
newPassword.length = 0;
14+
const characters = {
15+
symbols: "!@#$%^&*(){}[]=<>/,.",
16+
similar: "il1Lo0O",
17+
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
18+
lowercase: "abcdefghijklmnopqrstuvwxyz",
19+
numbers: "0123456789"
20+
};
21+
22+
const showSymbol = () => {
23+
const newPassword = [];
1624
hideCopiedMessage();
1725

1826
const lengthValue = +lengthElem.value,
@@ -22,40 +30,42 @@ function showSymbol() {
2230
hasUpperCase = addUpperCaseElem.checked,
2331
hasSimilar = addSimilarElem.checked;
2432

25-
const checkboxesCounter =
26-
hasSymbols + hasNumbers + hasLowerCase + hasUpperCase + hasSimilar;
33+
const options = [
34+
hasSymbols,
35+
hasNumbers,
36+
hasLowerCase,
37+
hasUpperCase,
38+
hasSimilar
39+
];
40+
41+
const noOptionChecked = options.every((option) => !option);
2742

28-
if (checkboxesCounter===0) {
43+
if (noOptionChecked) {
2944
passwordElem.value = "select an option";
3045
} else {
3146
while (newPassword.length < lengthValue) {
32-
if (hasSymbols) newPassword.push(getRandomSymbol());
33-
if (hasNumbers) newPassword.push(getRandomFromChartCode(10, 48));
34-
if (hasLowerCase) newPassword.push(getRandomFromChartCode(26, 97));
35-
if (hasUpperCase) newPassword.push(getRandomFromChartCode(26, 65));
36-
if (hasSimilar) newPassword.push(getRandomSimilar());
47+
for (const box in checkboxes) {
48+
if (checkboxes[box].checked)
49+
newPassword.push(getRandomCharacter(checkboxes[box].name));
50+
}
3751
}
38-
const generatedPassword = newPassword.join("");
39-
const finalPassword = generatedPassword.slice(0, lengthValue);
40-
passwordElem.value = finalPassword;
41-
}
4252

43-
lengthTextElem.textContent = lengthElem.value;
44-
}
53+
const shuffledFinalPassword = newPassword
54+
.sort((a, b) => 0.5 - Math.random())
55+
.join("")
56+
.slice(0, lengthValue);
4557

46-
function getRandomFromChartCode(quantity, startFrom) {
47-
return String.fromCharCode(Math.floor(Math.random() * quantity) + startFrom);
48-
}
58+
passwordElem.value = shuffledFinalPassword;
59+
}
4960

50-
function getRandomSymbol() {
51-
const symbols = "!@#$%^&*(){}[]=<>/,.";
52-
return symbols[Math.floor(Math.random() * symbols.length)];
53-
}
61+
lengthTextElem.textContent = lengthElem.value;
62+
};
5463

55-
function getRandomSimilar() {
56-
const similar = "il1Lo0O";
57-
return similar[Math.floor(Math.random() * similar.length)];
58-
}
64+
const getRandomCharacter = (optionName) => {
65+
return characters[optionName][
66+
Math.floor(Math.random() * characters[optionName].length)
67+
];
68+
};
5969

6070
const showCopiedMessage = () => (copyButtonTextElem.style.display = "block");
6171
const hideCopiedMessage = () => (copyButtonTextElem.style.display = "none");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /