函数不会返回值

时间:2017-08-01 20:31:29

标签: javascript return

我正在制作密码生成器。试图实现密码短语。

函数loadFile();不会将值返回到我的主函数generate();

我认为我太接近问题并无法解决问题。我可以使用一些帮助。谢谢。

测试网站:https://jf0.000webhostapp.com/passWordGenerator/ test.js来源:https://jf0.000webhostapp.com/passWordGenerator/test.js

来源变得有点长,对不起。

//Main generate password function
function generatePassword(){
    var x = document.getElementById("passOutput");
    var p = document.getElementById("testP");
    x.value = generate();
    //Make sure they aren't using an insecure number of characters
    checkMaxChars();
    p.innerText = x.value;
}

//Generate a password. 3 passSets for customization, one for passphrase
function generate(){
    var nL = document.getElementById("noLetters");
    var nN = document.getElementById("noNumbers");
    var nS = document.getElementById("noSymbols");
    var pPK = document.getElementById("passWordPhrase");
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var numbers = "0123456789";
    var symbols = "!@#$%^&*()_+~`|}{[]:;?,.-='";
    var maxChars = document.getElementById("maxCharControl").value;
    var generatedPass = "";
    var holdPass = "";
    //Main loop
    for(var i = 0;i < maxChars;i++){
        //Pick random value for each passSet
        var passSet1 = chars.charAt(Math.random() * chars.length);
        var passSet2 = numbers.charAt(Math.random() * numbers.length);
        var passSet3 = symbols.charAt(Math.random() * symbols.length);
        //if a checkbox is selected, clear out that value
        if(nL.checked == true){passSet1 = "";}
        if(nN.checked == true){passSet2 = "";}
        if(nS.checked == true){passSet3 = "";}
        //Randomly select a set to be added to holdPass, or not if it is empty.
        var r = Math.floor((Math.random() * 4) + 1);
        if(r == 1){if (passSet1 == ""){i--}else{holdPass += passSet1;}}
        if(r == 2){if (passSet2 == ""){i--}else{holdPass += passSet2;}}
        if(r == 3){if (passSet3 == ""){i--}else{holdPass += passSet3;}}
        if(r == 4){
        if(pPK.checked == true){
            //get the value from loadFile(); and apply it to passSet4, add that to holdPass.
        }}
    }
    generatedPass = holdPass;
    console.log("Max Characters:" + maxChars);
    console.log("passSet1:" + passSet1);
    console.log("passSet2:" + passSet2);
    console.log("passSet3:" + passSet3);
    console.log("Iteration:" + i);
    console.log("Random Position:" + r);
    console.log("Password:" + holdPass + "::::" + holdPass.length);
    //return password
    return generatedPass;
}

//Make sure they didn't select all the checkboxes
function checkBoxes(){
    var nL = document.getElementById("noLetters");
    var nN = document.getElementById("noNumbers");
    var nS = document.getElementById("noSymbols");
    var pP = document.getElementById("passWordPhrase");
    var nA = document.getElementById("noticeArea");
    var nT = document.getElementById("noticeAreaText");
    if(nL.checked == true && nN.checked == true && nS.checked == true){
        nL.checked = false;
        nN.checked = false;
        nS.checked = false;
        nA.style.display = "block";
        nT.innerHTML = "You cannot select all checkboxes at once.";
        window.setTimeout(hideNotice,5000);
    }
    else{
        nA.style.display = "none";
        nT.innerHTML = "";
    }
}
//make sure the max characters is greater than 8
function checkMaxChars(){
    var maxChars = document.getElementById("maxCharControl").value;
    var nA = document.getElementById("noticeArea");
    var nT = document.getElementById("noticeAreaText");
    var x = document.getElementById("passOutput");
    console.log(maxChars);
    if (maxChars < 8){
    x.value = "";
    nA.style.display = "block";
    nT.innerHTML = "You cannot generate a password less than 8 characters in length for security reasons.";
    window.setTimeout(hideNotice,7000);
    }
}
//hides the notice area div once the message is completed
function hideNotice(){
    var nA = document.getElementById("noticeArea");
    var nT = document.getElementById("noticeAreaText");
    nA.style.display = "none";
    nT.innerHTML = "";
}
//Load file
function loadFile() {
    var xhttp = new XMLHttpRequest();
    var x;
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        x = this.responseText;
        parseResponse(x);
        }
    }
    xhttp.open("GET", "wordList.csv", true);
    xhttp.send();
    return x;
}
//Format response
function parseResponse(x){
    console.log("MADE IT HERE!");
    var dS,sV1,rPos;
    dS = x.split(",");
    for(var i =0;i < dS.length;i++){
        sV1 = dS[i];
    }
    x = sV1;
}

2 个答案:

答案 0 :(得分:1)

在启动它的函数完成之后,AJAX请求的结果将永远不可用。您试图在启动请求的函数结束之前返回$indexer = Lucy::Index::Indexer->new( schema => $schema, index => $outIndex, create => 1, truncate =>0, ); ,这是在AJAX请求完成之前。

您需要将该行移动到您的AJAX成功处理程序中。

    public class Solution
    {
    public static int hourglass(int[][] a, int n)
    {
        int max = -999;
        for (int i = 0; i < n; i++)
        {         
            for (int j = 0; j < n; j++)
            {
                int sum = 0;
                boolean flag = false;

                if ((i+2) < n && (j+2) < n)
                {
                    sum += a[i][j] + a[i][j+1] + a[i][j+2] + a[i+1][j+1] + a[i+2][j] + a[i+2][j+1] + a[i+2][j+2];
                    flag = true;
                }
                if (sum > max && flag == true)
                    max = sum;
            }
        }
        return max;
    }
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        int n = 6;
        int[][] a = new int[6][6];

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = scanner.nextInt();

        int maxSum = hourglass(a, n);
        System.out.println(maxSum);
    }
}

答案 1 :(得分:0)

您的xhttp请求与true中的xhttp.open("GET", "wordList.csv", true);异步打开,因此在为其分配值之前会返回x。

编辑:同步说而不是异步,因为我很蠢