开发者

Function in JS returns undefined

开发者 https://www.devze.com 2022-12-24 19:34 出处:网络
I have the following problem. The alert allways returns undefined, but I know it has a value. What am I doing wrong. I\'m out of solutions...

I have the following problem. The alert allways returns undefined, but I know it has a value. What am I doing wrong. I'm out of solutions...

I'm using JQUERY jquery-1.4.2.min.js

Tnx in advance

$(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 {
  开发者_如何学C      //---- String is not "elf-proef", try again
        return false;
    }
}


This section:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    generateBankAccount();
}else{
    return  bankAccount;
}

If elfProef returns false then bankAccount will never be returned. It will simply run generateBankAccount which will return a value that never gets used. Try changing it to this:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    return generateBankAccount();
}else{
    return  bankAccount;
}


I take it generateBankAccount is a recursive function, right? If that's true you forgot to put "return" in front of the nested call.


I think you need to change the if else in generateBankAccount to read as follows:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    return generateBankAccount();
}else{
    return  bankAccount;
}

otherwise your recursive calls to generateBankAccount won't do anything.


Make this:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    generateBankAccount();
}

into this:

if (elfProef(bankAccount)==false) {
    //---- regenerate the string
    return generateBankAccount();
}

You could consider rewriting the function without recursion:

do {
    bankAccount = '';

    for (var i = 0; i < digitAmount; i++) {
        bankAccount += Math.floor(Math.random() * digitAmount);
    }    
}
while (!elfProef(bankAccount));

return bankAccount;

Using a loop makes the logic more straightforward and it doesn't chew up stack space in the case that you fail to generate a valid account number after a large number of tries.

0

精彩评论

暂无评论...
验证码 换一张
取 消