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 47076da

Browse files
moderate js constants, loops and names
1 parent f1db4f9 commit 47076da

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

‎day18/STARTER-FILES/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
min="6"
5151
max="32"
5252
/>
53-
<span id="lengthText">12</span> characters
53+
<span id="lengthText">12</span>&nbsp; characters
5454
</div>
5555

5656
<div class="field">

‎day18/app.js

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
const passwordElem = document.getElementById("password"),
22
lengthElem = document.querySelector("#length"),
33
lengthTextElem = document.querySelector("#lengthText"),
4-
addSymbolsElem = document.querySelector("#symbols"),
5-
addNumbersElem = document.querySelector("#numbers"),
6-
addLowerCaseElem = document.querySelector("#lowercase"),
7-
addUpperCaseElem = document.querySelector("#uppercase"),
8-
addSimilarElem = document.querySelector("#similar"),
94
copyButtonElem = document.querySelector(".copy"),
105
copyButtonTextElem = document.querySelector(".copy span");
116

@@ -19,41 +14,29 @@ const characters = {
1914
numbers: "0123456789"
2015
};
2116

22-
const showSymbol = () => {
23-
const newPassword = [];
24-
hideCopiedMessage();
25-
26-
const lengthValue = +lengthElem.value,
27-
hasSymbols = addSymbolsElem.checked,
28-
hasNumbers = addNumbersElem.checked,
29-
hasLowerCase = addLowerCaseElem.checked,
30-
hasUpperCase = addUpperCaseElem.checked,
31-
hasSimilar = addSimilarElem.checked;
17+
const generatePassword = () => {
18+
const passwordArray = [];
19+
const lengthRequired = +lengthElem.value;
20+
const options = Array.prototype.slice.call(checkboxes);
3221

33-
const options = [
34-
hasSymbols,
35-
hasNumbers,
36-
hasLowerCase,
37-
hasUpperCase,
38-
hasSimilar
39-
];
22+
const noOptionsChecked = options.every((option) => !option.checked);
4023

41-
constnoOptionChecked=options.every((option)=>!option);
24+
hideCopiedMessage();
4225

43-
if (noOptionChecked) {
26+
if (noOptionsChecked) {
4427
passwordElem.value = "select an option";
4528
} else {
46-
while (newPassword.length < lengthValue) {
47-
for (const box in checkboxes) {
48-
if (checkboxes[box].checked)
49-
newPassword.push(getRandomCharacter(checkboxes[box].name));
29+
while (passwordArray.length < lengthRequired) {
30+
for (const i in checkboxes) {
31+
if (checkboxes[i].checked)
32+
passwordArray.push(getRandomCharacter(checkboxes[i].name));
5033
}
5134
}
5235

53-
const shuffledFinalPassword = newPassword
36+
const shuffledFinalPassword = passwordArray
5437
.sort((a, b) => 0.5 - Math.random())
5538
.join("")
56-
.slice(0, lengthValue);
39+
.slice(0, lengthRequired);
5740

5841
passwordElem.value = shuffledFinalPassword;
5942
}
@@ -78,4 +61,4 @@ copyButtonElem.addEventListener("click", () => {
7861
showCopiedMessage();
7962
});
8063

81-
lengthElem.addEventListener("input", () => showSymbol());
64+
lengthElem.addEventListener("input", () => generatePassword());

0 commit comments

Comments
(0)

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