mardi 20 octobre 2020

How to call a function in an if-else statement?

Trying to call the pushChar function inside an if-else at the bottom, the alert will run but not the function. What am I doing wrong?

I have a feeling it's something to do with the scope but I'm very new to this so possible it's not.

It's a password generator that pulls from an array created by options chosen by the user. I want to unsure that all of the requirements are in the final password and if not, then re-run the function that creates the password.


// Write password to the #password input
function writePassword() {
    var password = generatePassword();
    var passwordText = document.querySelector("#password");

    passwordText.value = password;

}

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

//Generate password
function generatePassword() {

//finding out how many characters the user wants in the password with a set range of 8-128
var pwLength = parseInt(prompt("Enter a number between 8-128"));
  while (pwLength < 7 || pwLength > 129 || isNaN(pwLength) || pwLength === null) {
      alert("That's not a valid number. Please enter a number from 8-128.");
      pwLength = prompt("Enter a number between 8-128");
}

//Defining variables
var confirmLower = confirm("Do you want to include lower case characters?");
var confirmUpper = confirm("Do you want to include upper case characters?");
var confirmNumber = confirm("Do you want to include number characters?");
var confirmSpecial = confirm("Do you want to include special characters?");

//If all answers are false, looping back through to get atleast one true response.
while (confirmLower === false && confirmUpper === false && confirmNumber === false && confirmSpecial === false) {
    alert("You need to select one type of character");
    var confirmLower = confirm("Do you want to include lower case characters?");
    var confirmUpper = confirm("Do you want to include upper case characters?");
    var confirmNumber = confirm("Do you want to include number characters?");
    var confirmSpecial = confirm("Do you want to include special characters?");
}

// Various Character Arrays
var lowerCaseChar = ["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 upperCaseChar = ["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 numericChar = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
var specialChar = ["!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "{", "}", "|", "[", "]", ";", "'", ":", "<", ">", "?", "/"];

//Array created based on the answers to prompts by the user
var passwordPool = [];

//Creating new array containing the options(arrays) the user chose to include in the password
function generateChar() {
    if (confirmLower) {
        passwordPool.push(...lowerCaseChar);
    }
    if (confirmUpper) {
        passwordPool.push(...upperCaseChar);
    }
    if (confirmNumber) {
        passwordPool.push(...numericChar);
    }
    if (confirmSpecial) {
        passwordPool.push(...specialChar);
    }
}
    generateChar();
    console.log(passwordPool);

   //Creates final array out of random characters from the pool that was created by the users option inputs.
    function pushChar() {
        var randomPassword = [];
        for (var i = 0; i < pwLength; i++) {
            var item = passwordPool[Math.floor(Math.random() * passwordPool.length)];
            randomPassword.push(item);
    }
    return randomPassword;
}
var password = pushChar();


//validate that all of the conditions were met.

var checkUpper = (upperCaseChar.some(ele => password.includes(ele)))
var checkLower = (lowerCaseChar.some(ele => password.includes(ele)))
var checkNumeric = (numericChar.some(ele => password.includes(ele)))
var checkSpecial = (specialChar.some(ele => password.includes(ele)))

console.log(checkUpper);
console.log(checkLower);
console.log(checkNumeric);
console.log(checkSpecial);


if (checkUpper === confirmUpper &&
    checkLower === confirmLower &&
    checkNumeric === confirmSpecial &&
    checkSpecial === confirmNumber) {
    console.log(password);
} else {
        alert("somethings missing");
        console.log(pushChar()); //why won't this run??
}



//Presents randomly generated password to the user as a string. 
  return password.join("");
}

Aucun commentaire:

Enregistrer un commentaire