Function in JS returns undefined

Posted by megapool020 on Stack Overflow See other posts from Stack Overflow or by megapool020
Published on 2010-03-22T19:43:11Z Indexed on 2010/03/22 19:51 UTC
Read the original article Hit count: 261

Filed under:

Hello there,

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 {
        //---- String is not "elf-proef", try again
        return false;
    }
}

© Stack Overflow or respective owner

Related posts about jQuery