I know I have not been the best with coding. But right now I’m having a JavaScript issue. I’m trying to make a random password generator that when you check and uncheck certain boxes it will generate or not generate those specific checked/not-checked boxes. I’ve been trying to get this box to show the random generated password and see if it works but for some reason it will not. I do not know if the issue is because I have option1=> and option2=> but either ways when I delete one of those it still does not show. I’ve done multiple stuff like adding && and getting rid of another code block that is similar to the the options.forEach block. Using the exact code but except for option 1 and 2 it would just be the first block as option1 and the second block as option2. When I do console.log(placeholder)
it generates the text "Your secure password" which is the placeholder.
What are my mistakes and how could I get it to generate a password to the console.log
Every time I click the generate button all I get is this(screenshot).
Screenshot of Console.log
options = document.querySelectorAll(".option1 input")
const characters = { //object and letters
Lowercase: "abcdefghijklmnopqrstuvwxyz",
Uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
Numbers: "123456789",
Special: "@#$%&!_&"
}
const generatepassword = () => {
let password = "";
options.forEach(option1 => option2 => {
if (option1.checked && option2.checked) {
password += characters[option1.id && option2.id];
}
})
console.log(generatepassword);
}
// Get references to the #generate element
var generateBtn = document.querySelector("#generate");
// Write password to the #password input
// Add event listener to generate button
generateBtn.addEventListener("click", generatepassword);
<textarea readonly id="password" placeholder="Your Secure Password" aria-label="Generated Password"></textarea>
<div class="card-footer">
<button id="generate" class="btn">Generate Password</button>
</div>
</div>
</div>
<section class="options">
<section class="option1">
<div id="Uppercase">
<input type="checkbox" checked> Uppercase
</div>
<div id="Lowercase">
<input type="checkbox" checked> Lowercase
</div>
</section>
<section class="option2">
<div id="Special">
<input type="checkbox" checked> Special
</div>
<div id="Numbers">
<input type="checkbox" checked> Numbers
</div>
</section>
</section>
2
Answers
You don’t need to check the options in the loop. Use the selected options to produce a string that concatenates all the elements of
characters
that were selected. Then in the loop pick a random element from that combined string.There were a couple of issues in your code, including the HTML part and JavaScript, which were addressed in the comments of your question. I’ve tried to change them to represent what you were going for as so:
options
variable asNodeListOf<HTMLInputElement>
, because then when we iterate through the children we can check if the element is checked, since not all HTML element have this attribute. Theoptions
variable also didn’t have theconst
keyword, so I added that.for ... of
syntax instead of.forEach()
, but it’s pretty much the same thing. However, you had the issue of calling forEach with two arrow functions inoption1 => option2 => ...
. You can read up on what they are here, but I believe that’s not what you were going for.options
we have a loop variable calledoption
, and we don’t actually know which option this stands for in a given moment since it changes with each iteration of the loop. We just test if it’s checked and perform the appropriate operations accordingly. The code withoption1.id && option2.id
was in error because this would evaluate the two ids as booleans, applying the logical AND operator.password
variable you created to store the result. This was probably just a typo.As an unrelated note, I replaced
var
withconst
since it’s deprecated in modern JavaScript.Additionally, when adding the click event listener, I added optional chaining because if you choose to change the element ID in the HTML without updating the JavaScript, this line of code would throw an exception. Now, it evaluates to a no-op, and prevents the linter from writing up errors.