JS中的函数返回undefined

Meg*_*020 0 jquery

我有以下问题.警报allways返回undefined,但我知道它有一个值.我究竟做错了什么.我没有解决方案......

我正在使用JQUERY jquery-1.4.2.min.js

Tnx提前

$(document).ready(function(){
    $('#generateButton').click(createIBAN);
});

function createIBAN(){
    //---- First check if a bank has been selected,
    //---- if not, then show error
    if($('#selectBank').val()==''){
        alert('Selecte a bank!');
    }else{
        var bankAccount =   generateBankAccount();
        alert(bankAccount);
    }
    return false;
}

function generateBankAccount(){
    //---- Create "elfproef" bankaccount
    var bankAccount =   '';
    //---- Set the amount of digits in a bankaccount
    digitAmount     =   9;
    //---- Make random digitstring
    for (var i = 0; i < digitAmount; i++) {
        bankAccount += Math.floor(Math.random() * digitAmount);
    }
    //---- validate the string, if not "elf-proef"
    if (elfProef(bankAccount)==false) {
        //---- regenerate the string
        generateBankAccount();
    }else{
        return  bankAccount;
    }
}

function elfProef(bankAccount) {
    //---- set sum to 0 and start the for-loop for counting
    var sum = 0;
    for (var i = 0; i < digitAmount; i++) {
        //---- for every digit multiply it times 9 - number 
        //---- of the digit and count it to the sum var
        sum += bankAccount.charAt(i) * (digitAmount - i);
    }
    //---- Check if sum can be devided by 11 without having ##,##
    if(sum % 11==0){
        //---- return true means string is "elf-proef"
        return true; 
    }else {
        //---- String is not "elf-proef", try again
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

Bob*_*Bob 7

这个部分:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    generateBankAccount();
}else{
    return  bankAccount;
}
Run Code Online (Sandbox Code Playgroud)

如果elfProef返回false,则永远不会返回bankAccount.它将简单地运行generateBankAccount,它将返回一个永远不会被使用的值.尝试将其更改为:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    return generateBankAccount();
}else{
    return  bankAccount;
}
Run Code Online (Sandbox Code Playgroud)