如何从if-else语句中调用函数?

时间:2020-10-20 02:49:21

标签: javascript function if-statement scope

我试图在它下面的if-else语句中调用pushChar function()。执行后我将函数存储在其中的变量是全局变量,所以为什么不能调用它?

//Creates final array out of random characters from the arraypool that was created by the users criteria 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");
    pushChar(); //why won't this run??
}



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

}

0 个答案:

没有答案