Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript how do I ensure at least 1 variable from each array is included in the final password?

brizzaro

New Coder
JavaScript:
// global variables
var number = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var special = ["!", "%", "&", ",", "*", "+", "-", ".", "/", "<", ">", "?","~"];
var lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

var passwordOption = []
var passwordText = ""
// Get references to the #generate element
var generateBtn = document.querySelector("#generate");

  function getPassword() {

  var charLength = (prompt("Choose length of password between 8-128 characters"));
 
 
  // password length with prompt to get user to choose correct length
 
  while (charLength < 8 || charLength > 128 || isNaN(charLength)) {
    alert("please enter a number between 8-128 characters");
    return getPassword();
  }
 
  // password character options
  do {var confirmNumber = confirm("Click OK for Numbers");
    var confirmSpecial = confirm("Click OK for Special Characters");
    var confirmLower = confirm("Click OK for Lower Case Letters");
    var confirmUpper = confirm("Click OK for Upper Case Letters");

  //while loop to force option selection
    if (confirmNumber === false && confirmSpecial === false && confirmLower === false && confirmUpper === false) {
      alert("One option required")
    }}
    while(confirmNumber === false && confirmSpecial === false && confirmLower === false && confirmUpper === false)

    // if conditionals to build array based on user input
  if (confirmNumber) {
    passwordOption = passwordOption.concat(number)
  }

  if (confirmSpecial) {
    passwordOption = passwordOption.concat(special)
  }

  if (confirmLower) {
    passwordOption = passwordOption.concat(lower)
  }

  if (confirmUpper) {
    passwordOption = passwordOption.concat(upper)
  }
     
//  for loop to generate password from pool of user selected characters
var randomPassword = ""
      for (var i = 0; i < charLength; i++) {
        randomPassword = randomPassword + passwordOption[Math.floor(Math.random() * passwordOption.length)];
      }
      return randomPassword;
}

// write password to text box
function writePassword(){
var password = getPassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}

// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);
 
Last edited by a moderator:
Basic code to check if a string contains an element of an array, you should be able to build on this:
JavaScript:
const password = 'verySecureP@ssw0rd';
const numbers = ['1', '2', '3'];

const contains = numbers.some(e => {
  if (password.includes(e)) {
    return true;
  }
  return false;
});
 

New Threads

Buy us a coffee!

Back
Top Bottom