ringocub
ringocub

Reputation: 457

Why does the following javascript function always return true?

I have the following function, it will always return True. Any ideas why and how to avoid it? Thanks folks.

function validateStatuses(xyx){
var umm = ugh[xyx];
var selects = $('#cont_'+ugh.xyz+' .status_select');
var codes = $('#cont_'+ugh.xyz+' .status_code');
for (var i = 0; i < selects.length; i++) {
    var value = selects[i].options[selects[i].selectedIndex].value;
    if (value == 'new'){
        for (var j = 0; j < codes.length; j++) {
            var blagh = codes[j].options[codes[j].selectedIndex].value;
            if(blagh == 13){
                $('#info_dialog').html('');
                $('#info_dialog').append("<p>You are trying to process a bill ("+bill.name+") with a STATUS of NEW and a STATUS CODE of NONE. Please correct this issue before you proceed!</p><hr />");
                $('#info_dialog').dialog({
                    buttons:{
                        Cancel: function(){
                            $(this).dialog('close');
                        }
                    }
                    });
                billCounterAdd();
                return false;
            }//end if           
        }//end for
    }else{
        return true;  //this is the problem;
    }//end if
}//end for
}//end Function

Upvotes: 0

Views: 802

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1073988

I dare say you have at least one select whose value isn't 'new'. Because you've done a return true; in the else clause, the first select with a value that isn't 'new' will cause the function to return true.

It looks like it does have a false return route (if there's a 'new' select at the beginning and there's a code select with the value 13), but perhaps that test case didn't come up in your testing.

In terms of figuring out what's wrong with things like this, there's nothing quite like walking through the code and watching it run line-by-line in a decent debugger. All major browsers have them built in now (finally), so you can see exactly what's happening and inspect variables, etc.

Upvotes: 3

Related Questions