0

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
3
  • 1
    It returns true because, at some point, if (value == 'new') will have the condition evaluated to false, hence driving code flow to the else branch. Commented Jan 9, 2012 at 15:14
  • 1
    Any time you have to comment the closing "}" of a block it usually means the block is too long. Commented Jan 9, 2012 at 15:14
  • Off-topic, but as you seem to be using jQuery, you might look at its val and each functions, which might help simplify the code. Commented Jan 9, 2012 at 15:17

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Right on fine sir and a mighty fine thank you to everyone for the prompt response. I felt rather red faced after reading your description. I moved the return true statement outside of the last For loop to get the desired effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.