Home > database >  Wanted to put user input into function
Wanted to put user input into function

Time:10-04

I am wanting to put my user prompt inputs into my password generator. So far everything else works except for the user inputs. The only one I have working so far is length. If you see the userDigits input directly below links that is where I am getting stuck now. Not sure if the choseNo empty box choice is doing what I want it to do. I want them to type yes or no and get digits or not in there chosen length of password. Then I will repeat for userSpec, userLower, and userUpper. I have been trying to get this for the last 24 hours. The only help I can find is for confirm prompts and check boxes. I just want the user to be able to type them in and go on to next question and force a loop back if the answer is not yes or no lowercase or capital. Then finally I want to be able to put those into the password itself. All help is appreciated. Thank you.

// Assignment Code
var generateBtn = document.querySelector("#generate");

const allowedDigits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']; 
const allowedUpperCase = ['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'];
const allowedLowerCase = ['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'];
const allowedSpecial = ['!', '@' , '#', '$', '%', '^', '&', '*', '?'];
const choseNo = [];

function getRandomCharacter(array) {
  return array[Math.floor(Math.random() * array.length)];
}


function generatePassword(length, allowedCharacterSets) {
  var password = "";

  for(let i = 0; i < allowedCharacterSets.length;   i) {

  }

  for (var i = 0; i < length; i  ) {
    password  = getRandomCharacter(allowedCharacterSets[i % allowedCharacterSets.length]);
  }
  const UNIVERSAL_CHARACTER_SET = allowedCharacterSets.flat();

  for (let i = password.length;  i < length;   i) {
    password  = getRandomCharacter(UNIVERSAL_CHARACTER_SET);
  }

  return password; 

}


// Write password to the #password input
function writePassword() {

    let isValidLength = false;
    let promptText = "How long would you like your password to be? Please choose between 8 and 128 characters."
    while (!isValidLength){
      var length = window.prompt(promptText);
      if (parseInt(length).toString() !== 'NaN'){
           if (length >= 8 && length <= 128){
             isValidLength = true;
           } else {
               promptText = 'Invalid option. Please enter a number between 8 and 128.';
           }
           
      }
    }

    var userDigits = window.prompt("Would you like to include numbers in your password? Type yes or no.");
      if (userDigits === "" || userDigits === null){
       var userDigits = window.prompt("Would you like to include numbers in your password? Type yes or no.");
        }
     userDigits = userDigits.toLocaleLowerCase();
      if (userDigits === "no") {
        userDigits = choseNo;
      }
        if (userDigits === "yes") {
          userDigits = allowedDigits;
          }
   
    var userSpec = window.prompt("Would you like to include special characters in your password? Type yes or no.");
    var userLower = window.prompt("Would you like to include lowercase letters in your password? Type yes or no.");
    var userUpper = window.prompt("Would you like to include uppercase letters in your password? Type yes or no.");
  
  var password = generatePassword(length, [allowedDigits, allowedSpecial, allowedLowerCase, allowedUpperCase]);
  var passwordText = document.querySelector("#password");

  passwordText.value = password;

}

// Add event listener to generate button
generateBtn.addEventListener("click", writePassword);

/**
 *  @return an array of allowed character arrays
 */

 function promptUser() {
  
}

CodePudding user response:

I didn't add any prompt validation, or make it okay to click cancel, but here is an example of how to accomplish this idea.

function main() {
  const parameters = getParameters();
  const pass = generatePass(parameters);
  console.log(`
    parameters = ${
      Object.entries(parameters)
      .reduce((a,[key, val]) => {
        return a   key   " : "   val   "\n"
      }, "\n\n")
    }
    pass is : ${pass}
    len is : ${pass.length}
  `);
}

function getParameters() {
  const lenRes = prompt("How long would you like your password to be? Please choose between 8 and 128 characters.");
  const len = parseInt(lenRes);

  const hasNumRes = prompt("Would you like to include numbers in your password? Type yes or no.");
  const hasNum = hasNumRes.toLowerCase().includes("y");

  const hasSpecRes = prompt("Would you like to include special characters in your password? Type yes or no.");
  const hasSpec = hasSpecRes.toLowerCase().includes("y");

  const hasLowerRes = prompt("Would you like to include lowercase letters in your password? Type yes or no.");
  const hasLower = hasLowerRes.toLowerCase().includes("y");

  const hasUpperRes = prompt("Would you like to include uppercase letters in your password? Type yes or no.");
  const hasUpper = hasUpperRes.toLowerCase().includes("y");

  return {len, hasNum, hasSpec, hasLower, hasUpper}
}

function generatePass(parameters) {
  const {len} = parameters;
  const charList = getCharList(parameters);
  let pass = "";
  for (let i = len; i --> 0 ;){
    pass  = getRandomChar(charList);
  }
  return pass;
}

function getCharList({hasNum, hasSpec, hasLower, hasUpper}) {
  const allowedChars = [];
  const potentialDigits = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
  const potentialUpperCase = ['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'];
  const potentialLowerCase = ['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'];
  const potentialSpecial = ['!', '@' , '#', '$', '%', '^', '&', '*', '?'];
  if (hasNum) allowedChars.push(...potentialDigits);
  if (hasSpec) allowedChars.push(...potentialSpecial);
  if (hasLower) allowedChars.push(...potentialLowerCase);
  if (hasUpper) allowedChars.push(...potentialUpperCase);
  return allowedChars;
}

function getRandomChar(list) {
  const randChar = list[Math.floor(Math.random() * list.length)];
  return randChar
}

main();

  • Related