random password generator validation

Viewed 340

I'm new to coding and working in JS on a random password generator and need to have user input to validate at least one char. I have my prompt and confirms working but unsure how to validate note;

My if statement and for loop are dimmed. The userSelection array is working when I console.log it.

var upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowerCase = "abcdefghijklmnopqrstuvwxyx";
var numbers = "0123456789";
var specialChar = "!@#$%^&*()?/;:~";

//added userSelection & finalPassword var
var userSelection =[]; 

var finalPassword = [];

//added functions, prompt, console.log
function generatePassword() { 

  
  var passLength = prompt("Please insert a number between 8 to 128");
  //remember to add a condition where IF  the user choose less than 8 or greater than 128 to stop and alert them to follow instruction. Then ask initial question again 
  if (passLength <8 ) {
    alert ("Please insert a number between 8 to 128");
    return;
  }
  if (passLength >128 ) {
  alert ("Please insert a number between 8 to 128");
  return;
  }
  
  //added upperCase/lowerCase confirm var
  var upperC = confirm("Would you like Uppercase Letters");
  if(upperC) {
    userSelection.push(upperCase);
  } 
  
  var lowerC = confirm("Would you like Lowercase Letters");
  if(lowerC) {
    userSelection.push(lowerCase);
  }
  //added remaining specChar and numb var
  var specC = confirm("Would you like and Special Characters");
  if(specC) {
    userSelection.push(specialChar);
  }

  var numb = confirm("Would you like to add Numbers to your password?"); 
  if(numb) { 
    userSelection.push(numbers);
  }
  console.log("user selection = " + userSelection);

  //added if statement) NEED definition of what this is doing
  if (userSelection.length ===0);
  return;

  // for loop with random index to create random char 
  for (var i = 0; i < userSelection; i++) {
    var randomIndex = Math.floor(Math.random() * userSelection);
    var random = userSelection.charAt(randomIndex); 
  }
  //var math.floor will pick a random index for userSelection array set to var
  //math.floor to pick random char  --name as var use this to concat
}
2 Answers

Your problems lies in these lines:

if (userSelection.length === 0);
return;

What I think you wanted to do here, was to exit the function (with return) when userSelection is empty. This is almost the code for that...

However, because you have a semicolon ; at the end of the if statement line, you have broken your code to two separate statements:

  • if userSelection is empty, do nothing.
  • exit the function.

To fix this, you simply need to drop the semicolon:

if (userSelection.length === 0)
  return;

One way to avoid problems like this is to always use curly-braces in your if statement (like you've done with your other if-statements):

if (userSelection.length === 0) {
  return;
}

If I understood your logic correctly, your program must generate a password having the length of the size specified in the first question.

If that's it then do the spell

check

const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerCase = "abcdefghijklmnopqrstuvwxyx";
const numbers = "0123456789";
const specialChar = "!@#$%^&*()?/;:~";

//added functions, prompt, console.log
function generatePassword() { 

    //added userSelection & finalPassword var
  var userSelection = ''; 
  var finalPassword = '';
  
  var passLength = prompt("Please insert a number between 8 to 128");
  //remember to add a condition where IF  the user choose less than 8 or greater than 128 to stop and alert them to follow instruction. Then ask initial question again 
  if (passLength <8 ) {
    alert ("Please insert a number between 8 to 128");
    return;
  }
  if (passLength >128 ) {
  alert ("Please insert a number between 8 to 128");
  return;
  }
  
  //added upperCase/lowerCase confirm var
  var upperC = confirm("Would you like Uppercase Letters");
  if(upperC) {
    userSelection = userSelection.concat(upperCase);
  } 
  
  var lowerC = confirm("Would you like Lowercase Letters");
  if(lowerC) {
    userSelection = userSelection.concat(lowerCase);
  }
  //added remaining specChar and numb var
  var specC = confirm("Would you like and Special Characters");
  if(specC) {
    userSelection = userSelection.concat(specialChar);
  }

  var numb = confirm("Would you like to add Numbers to your password?"); 
  if(numb) { 
    userSelection = userSelection.concat(numb);
  }
  console.log("user selection = " + userSelection);

  //added if statement) NEED definition of what this is doing
  if (userSelection.length ===0)   return;

  // for loop with random index to create random char 
  for (var i = 0; i < passLength ; i++) {
    var randomIndex = Math.floor(Math.random() * userSelection.length);
    var random = userSelection[randomIndex]; 
    finalPassword = finalPassword.concat(random)
  }

return finalPassword
}

const resp = generatePassword()

console.log(`Password :::> ${resp} | length :::> ${resp.length}`)

Related