-2

I,ve been stuck on this problem for several days now. I cant seem to get the full password length into the box it only gives me one character. its currently not putting a password in the box as intended and am not sure what im missing., i think my eyes are bleeding from staring at this thing for so long.

   var generateBtn = document.querySelector("#generate");
var lowerCase = ("abcdefghijklmnopqrstuvwxyz").split("");
var upperCase = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ").split("");
var numbers = ("0123456789") .split("");
var specialChar = ("!@#$%^&*") .split("");
var get = document.querySelector("#generate");

var choices;

get.addEventListener("click", function () {
    ps = generatePassword();
    document.getElementById("password").placeholder = ps;
});
// Write password to the #password input
function generatePassword() {
  // Asks for user input
  enter = parseInt(prompt("How many characters would you like your password? Choose between 8 and 128"));
  // First if statement for user validation 
  if (!enter) {
      alert("This needs a value");
  } else if (enter < 8 || enter > 128) {
      // Validates user inputvar
      // Start user input prompts
      enter = parseInt(prompt("You must choose between 8 and 128"));

  } else {
      // Continues once user input is validated
      numbers = confirm("Will this contain numbers?");
      specialChar = confirm("Will this contain special characters?");
      upperCase = confirm("Will this contain uppercase letters?");
      lowerCase = confirm("Will this contain lowercase letters?");
  };

  // Else if for 4 negative options
  if (!specialChar && !numbers && !upperCase && !lowerCase) {
      choices = alert("You must choose something to put in!");

  }
  // First if statement that uses user input prompts to determine choices
  // Else if for 4 positive options
  else if (specialChar && numbers && upperCase && lowerCase) {

      choices = specialChar.concat(number, upperCase, lowerCase, specialChar);
  }
  // Else if for 3 positive options
  else if (specialChar && numbers && upperCase) {
      choices = specialChar.concat(numbers, upperCase, specialChar);
  }
  else if (specialChar && numbers && lowerCase) {
      choices = specialChar.concat(numbers, lowerCase);
  }
  else if (specialChar && lowerCase && upperCase) {
      choices = specialChar.concat(lowerCase, u);
  }
  else if (numbers && lowerCase && upperCase) {
      choices = numbers.concat(lowerCase, upperCase);
  }
  // Else if for 2 positive options 
  else if (specialChar && numbers) {
      choices = specialChar.concat(numbers);

  } else if (specialChar && lowerCase) {
      choices = specialChar.concat(lowerCase);

  } else if (specialChar && upperCase) {
      choices = specialChar.concat(upperCase);
  }
  else if (lowerCase && numbers) {
      choices = lowerCase.concat(numbers);

  } else if (lowerCase && upperCase) {
      choices = lowerCase.concat(upperCase);

  } else if (numbers && upperCase) {
      choices = numbers.concat(upperCase);
  }
  // Else if for 1 positive option
  else if (specialChar) {
      choices = specialChar;
  }
  else if (numbers) {
      choices = numbers;
  }
  else if (lowerCase) {
      choices = lowerCase;
  }

  else if (confirmUppercase) {
      choices = upperCase;
  };

  // password variable is an array placeholder for user generated amount of length
  var password = [];

  // Start random selection variables:
  // Random selection for all variables: 
  for (var i = 0; i < enter; i++) {
      var pickChoices = choices[Math.floor(Math.random() * choices.length)];
      password.push(pickChoices);
  }
  // This joins the password array and converts it to a string
  // Worked with a tutor to incorporate this option
  var ps = password.join("");
  UserInput(ps);
  return ps;
}
// Ad event listener to generate button
generateBtn.addEventListener("click", writePassword);

1 Answers1

1

You don't want to randomly fill a random character in your password. What you want is to fill each consecutive character in password[i] with a random that fits your acceptable password characters.

Something like this https://stackoverflow.com/a/9719815/2955337

sleepyhead
  • 390
  • 1
  • 9