Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    182
    Thanks
    139
    Thanked 0 Times in 0 Posts

    frustration with the if() statement

    Hi there,

    I need your help

    There must be a cleaner, clever and neater way than attempting to do what I am doing. I am trying to basically compare vars in a function to do different things when values are either specified or not:

    Code:
    function test() {
    
    var reqtype = "REQUEST"
    	reqtype = reqtype.length
    	
    var doctype = "DOCUMENT"
    	doctype = doctype.length
    	
    var status = "ACTIVE"
    	status = status.length
    	
    var routing = "MAIL"
    	routing = routing.length
    
    if 		(status > 0 && routing > 0) 								{ alert("all 2a groups") }
    if 		(reqtype > 0 && routing > 0) 								{ alert("all 2b groups") }
    
    if 		(reqtype > 0 && status > 0 && doctype > 0 && routing > 0)	{ alert("all 4 groups") }
    else if (reqtype > 0 && status > 0 && doctype > 0)					{ alert("all 3a groups") }
    
    if 		(reqtype > 0 && status > 0 && routing > 0)					{ alert("all 3b groups") }
    
    }//end of function
    With the current set of values given in the vars, it seems to set off everything else. How can I get the only line:
    Code:
    if 		(reqtype > 0 && status > 0 && doctype > 0 && routing > 0)	{ alert("all 4 groups") }
    to trigger and not the rest?

    Much thanks and appreciation for all your help.

    Jay

  • #2
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Hi Kelly,
    Longest condition first and the shortest last, try to use "switch" then "if" when you have multi condition(more then 3); other then this, I leave it up to you to develop your own style
    Code:
    	switch(true){
    		case  reqtype > 0 && status > 0 && doctype > 0 && routing > 0:
    			alert("all 4 groups") ;
    		break;
    		case reqtype > 0 && status > 0 && doctype:
    			alert("all 3a groups") ;
    		break;
    		case reqtype > 0 && status > 0 && routing:
    			alert("all 3b groups") ;
    		break;
    		case reqtype > 0 && routing > 0:
    			alert("all 2b groups");
    		break;
    		case status > 0 && routing > 0:
    			alert("all 2a groups") ;
    		break;
    	}

  • Users who have thanked Richter for this post:

    jason_kelly (06-17-2012)

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,903
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Something else you can do to simplify coding is to set flags and unset them if a condition fails.

  • Users who have thanked DrDOS for this post:

    jason_kelly (06-17-2012)

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    Sheffield, UK
    Posts
    138
    Thanks
    81
    Thanked 1 Time in 1 Post
    Quote Originally Posted by jason_kelly View Post
    Hi there,

    I need your help

    There must be a cleaner, clever and neater way than attempting to do what I am doing. I am trying to basically compare vars in a function to do different things when values are either specified or not:

    Code:
    function test() {
    
    var reqtype = "REQUEST"
        reqtype = reqtype.length
        
    var doctype = "DOCUMENT"
        doctype = doctype.length
        
    var status = "ACTIVE"
        status = status.length
        
    var routing = "MAIL"
        routing = routing.length
    
    if         (status > 0 && routing > 0)                                 { alert("all 2a groups") }
    if         (reqtype > 0 && routing > 0)                                 { alert("all 2b groups") }
    
    if         (reqtype > 0 && status > 0 && doctype > 0 && routing > 0)    { alert("all 4 groups") }
    else if (reqtype > 0 && status > 0 && doctype > 0)                    { alert("all 3a groups") }
    
    if         (reqtype > 0 && status > 0 && routing > 0)                    { alert("all 3b groups") }
    
    }//end of function
    With the current set of values given in the vars, it seems to set off everything else. How can I get the only line:
    Code:
    if         (reqtype > 0 && status > 0 && doctype > 0 && routing > 0)    { alert("all 4 groups") }
    to trigger and not the rest?

    Much thanks and appreciation for all your help.

    Jay
    I'm a bit confused as to what you're trying to do here. Could you explain in a bit more detail, what you want your script to do and how you want it to behave? If I have a clear idea of what you want to do, I may be able to help you.
    http://www.topcashback.co.uk/ref/hashim1

    ^
    Total earnings so far: £25.15
    A very generous cashback site worth checking out.


  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,065
    Thanks
    36
    Thanked 498 Times in 492 Posts

    Lightbulb

    I don't think you are checking what you think you are.
    For example:
    Code:
    <script type="text/javascript">
    var reqtype = "REQUEST"
    	reqtype = reqtype.length
    	
    var doctype = "DOCUMENT"
    	doctype = doctype.length
    
    alert(reqtype+'\n'+doctype);
    
    </script>
    The alert will always return 7 and 8 because that's the length of the variable strings
    before they are turned into numbers with the .length command

    The 'if ...' test will not change the results.

    Also, for what it's worth, if you are after the length of the string you could just as easily do this in one step...
    Code:
    <script type="text/javascript">
    var reqtype = "REQUEST".length
    	
    var doctype = "DOCUMENT".length
    
    alert(reqtype+'\n'+doctype);
    
    </script>

  • Users who have thanked jmrker for this post:

    jason_kelly (06-17-2012)

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Posts
    182
    Thanks
    139
    Thanked 0 Times in 0 Posts
    Thanks very much Richter!

    I didn't think of ever using the switch / case method.

    But it works the best as I adjust the variables accordingly.

    Simply flawless.

    Thank you too to the many others who have helped me.

    Cheers and have an awesome day!

    Jay


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •