首页 > 解决方案 > 如何从 if-else 语句中调用函数?

问题描述

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

//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("");

}

标签: javascriptfunctionif-statementscope

解决方案


推荐阅读