Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-15-2012, 06:42 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
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
jason_kelly is offline   Reply With Quote
Old 06-15-2012, 10:13 PM   PM User | #2
Richter
New Coder

 
Join Date: Jun 2012
Posts: 63
Thanks: 0
Thanked 11 Times in 11 Posts
Richter is an unknown quantity at this point
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;
	}
Richter is offline   Reply With Quote
Users who have thanked Richter for this post:
jason_kelly (06-17-2012)
Old 06-15-2012, 10:37 PM   PM User | #3
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,234
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
Something else you can do to simplify coding is to set flags and unset them if a condition fails.
DrDOS is offline   Reply With Quote
Users who have thanked DrDOS for this post:
jason_kelly (06-17-2012)
Old 06-16-2012, 01:07 AM   PM User | #4
Hashim1
Regular Coder

 
Join Date: Dec 2010
Location: Sheffield, UK
Posts: 136
Thanks: 81
Thanked 1 Time in 1 Post
Hashim1 is an unknown quantity at this point
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.

Hashim1 is offline   Reply With Quote
Old 06-16-2012, 03:02 PM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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>
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
jason_kelly (06-17-2012)
Old 06-17-2012, 08:08 PM   PM User | #6
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
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
jason_kelly is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:40 AM.


Advertisement
Log in to turn off these ads.