Question

Troubleshooting a comparison

Asked by: Leprechaun

I'm pulling some values from an array and trying to parse them a particular way.
When I use snippet #1, the debugvar variable gets populated as follows:
A:A
B:B
D:C
D:D
E:E
G:F
G:G
[etc]

I want to only diplay info when the 2 values are the same, so I tried adding an IF statement to skip dismiliar pairs (e.g., D:C & G:F; see Snippet #2), but for some reason I'm now only getting a small subset of the results of Snippet #1. I only get the following:
A:A
B:B

I don't understand how these variations could be happening. It's all being pulled from an array and the only difference between the two is that IF statement (and, besides, shouldn't a glitch cause *all* comparisons to fail?).

Thanks.

// Snippet #1
for (var x = 1; x < topA.length; x++) 
{
	var debugvar=topA[x - 1] + ':' + currentCat2 +':'+''+'';
	box1.options[menuNums-1] = new Option(debugvar, menuNums);
}
 
//Snippet #2
for (var x = 1; x < topA.length; x++) 
{
	if(topA[x - 1]==currentCat2)
	{
		var debugvar=topA[x - 1] + ':' + currentCat2 +':'+''+'';
		box1.options[menuNums-1] = new Option(debugvar, menuNums);
	}
}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-06-10 at 12:10:39ID24480608
Tags

JavaScript

Topic

JavaScript

Participating Experts
1
Points
250
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. par file
    Waht is a file with .par used for and what does this extension mean? Can I list the parameters for import in a .par file and use this file in imp, like imp file=filename.par Please suggest.
  2. par io err laserjet 4 plus
    Every time I start my computer my old laserjet 4 plus comes up with an error message on the control panel "err par io" the work around is to cycle power on the printer after the computer is loaded. This problem started when I got a new computer. I have tried chan...
  3. PAR Protocol
    How has the PAR Protocol been extended for use in the Internet?
  4. Overlapping/Subset Dates query.
    Hi experts, below is my table. 1 101 01/Jan/2004 30/Jan/2004 2 101 10/Jan/2004 30/Jan/2004 3 101 15/Jan/2004 29/Jan/2004 4 102 02/Feb/2004 05/Feb/2004 5 102 03/Feb/2004 07/Feb/2004 6 101 25/Jan/2004 10/Feb/2004 7 101 01/Feb/2004 10/Feb/2004 8 102 01/Jan/2004 02/Feb/2004 9 1...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: HonorGodPosted on 2009-06-10 at 12:32:10ID: 24595322

The values may be different because of leading, or trailing spaces, or non-displayable characters.

 

by: HonorGodPosted on 2009-06-10 at 12:34:59ID: 24595363

You could verify this by testing using something like:

for (var x = 1; x < topA.length; x++) 
{
  var debugvar = '"' + topA[x - 1] + '":"' + currentCat2 +'":'+''+'';
  box1.options[menuNums-1] = new Option(debugvar, menuNums);
}

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: LeprechaunPosted on 2009-06-10 at 12:40:12ID: 24595438

Yeah, I thought of that. That's why I have a ":" after each, to check for whitespace. There isn't any. (And the two variables are being pulled from the same value in an array, so there couldn't be any.)

Also, even if there were whitespace, the whitespace would be consistent in both cases, so that doesn't explain why D:D would appear in Snippet #1 but not in Snippet #2.

Tx.

// BTW, here's a snippet of the array involved.
 
topTobacco='Tobacco Use'; 	
topAlcohol='Alcohol Consumption';
topOHealth='Oral Health'; 	
topImmuns='Immunizations'; 	
topWomHealth='Women Health'; 
topProsCanc='Prostate Cancer Screening';
topColoCanc='Colorectal Cancer Screening';
topFirearm='Firearm'; 		
topIPViol='Intimate Partner Violence';
topSexViol='Sexual Violence'; 
topFruitVeg='Fruits and Vegetables';
topSleep='Sleep'; 			
topSBelt='Seat Belt Use'; 	
	
topA = new Array(); varA = new Array(); yearA = new Array(); valA = new Array(); menuA = new Array();
 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='SEX'; varA[varA.length]='Gender of respondent'; 
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008';
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='age_ok'; varA[varA.length]='What is your age? (3 groups)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='ageg_ok'; varA[varA.length]='What is your age? (7 groups)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='race_ok'; varA[varA.length]='What is your race/ethnicity?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='educa_ok';  varA[varA.length]='What is the highest level of education completed?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo;  menuA[menuA.length]=''; valA[valA.length]='income_ok'; varA[varA.length]='What is your annual household income?';
	yearA[yearA.length]='1988,1989,1990,1991,1992'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='marital'; varA[varA.length]='What is your marital status?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:

Select allOpen in new window

 

by: LeprechaunPosted on 2009-06-10 at 12:44:15ID: 24595481

It's really weird.

 

by: HonorGodPosted on 2009-06-10 at 12:52:21ID: 24595574

What is in topDemo?

 

by: LeprechaunPosted on 2009-06-10 at 13:07:13ID: 24595753

Okay, please see below.

So, the problem is that with Snippet #1 (w/o the IF statement), I get
> "Demographics":"Demographics":
> "Geography":"Geography":
> "Health Status":"Year":
> "Health Status":"Health Status":
> [etc.]

But with Snippet #2 (w/ the IF statement to eliminate dissimilar pairs), I get
> "Demographics":"Demographics":
> "Geography":"Geography":
[nothing else]

It doesn't make sense to me. I don't see how that IF statement should affect anything.

Thanks.

//THE ARRAY POPULATING CODE INVOLVED
topDemo = "Demographics"; 	
topGeo='Geography'; 		
topYear='Year'; 			
topHStat='Health Status'; 	
topHCA='Health Care Access'; 
topDiab='Diabetes'; 		
topArth='Arthritis'; 		
topCardio='Cardiovascular Disease';
topHyper='Hypertension Awareness';
topAsthma='Current Asthma'; 
topChol='Cholesterol'; 		
topWeight='Weight Control'; 
topTobacco='Tobacco Use'; 	
topAlcohol='Alcohol Consumption';
topOHealth='Oral Health'; 	
topImmuns='Immunizations'; 	
topWomHealth='Women Health'; 
topProsCanc='Prostate Cancer Screening';
topColoCanc='Colorectal Cancer Screening';
topFirearm='Firearm'; 		
topIPViol='Intimate Partner Violence';
topSexViol='Sexual Violence'; 
topFruitVeg='Fruits and Vegetables';
topSleep='Sleep'; 			
topSBelt='Seat Belt Use'; 	
	
topA = new Array(); varA = new Array(); yearA = new Array(); valA = new Array(); menuA = new Array();
 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='SEX'; varA[varA.length]='Gender of respondent'; 
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008';
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='age_ok'; varA[varA.length]='What is your age? (3 groups)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='ageg_ok'; varA[varA.length]='What is your age? (7 groups)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='race_ok'; varA[varA.length]='What is your race/ethnicity?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='educa_ok';  varA[varA.length]='What is the highest level of education completed?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topDemo;  menuA[menuA.length]=''; valA[valA.length]='income_ok'; varA[varA.length]='What is your annual household income?';
	yearA[yearA.length]='1988,1989,1990,1991,1992'; 
topA[topA.length]=topDemo; menuA[menuA.length]=''; valA[valA.length]='marital'; varA[varA.length]='What is your marital status?';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topGeo; menuA[menuA.length]=''; valA[valA.length]='CTYCODE'; varA[varA.length]='Residence (County)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topGeo; menuA[menuA.length]=''; valA[valA.length]='Region'; varA[varA.length]='Residence (Region)';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topYear; menuA[menuA.length]=''; valA[valA.length]='Year'; varA[varA.length]='Year';
	yearA[yearA.length]='1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2007'; //TEMP 2008
topA[topA.length]=topHStat; menuA[menuA.length]=''; valA[valA.length]='GENHLTH'; varA[varA.length]='How is your general health? ( 2 groups)';
	yearA[yearA.length]='1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topHStat; menuA[menuA.length]=''; valA[valA.length]='PHYSHLTH'; varA[varA.length]='Number of physically unhealthy days (last month)';
	yearA[yearA.length]='1993,1994,1995,1996,1997,1998,1999,2000,2001,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topHStat; menuA[menuA.length]=''; valA[valA.length]='MENTHLTH'; varA[varA.length]='Number of mentally unhealthy days (last month)';
	yearA[yearA.length]='1993,1994,1995,1996,1997,1998,1999,2000,2001,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topHStat; menuA[menuA.length]=''; valA[valA.length]='poorh_ok'; varA[varA.length]='Number of days poor health restrict activity (last month)';
	yearA[yearA.length]='1993,1994,1995,1996,1997,1998,1999,2000,2001,2003,2004,2005,2006,2007,2008'; 	 	
topA[topA.length]=topHCA; menuA[menuA.length]=''; valA[valA.length]='hlthpln'; varA[varA.length]='Do you have any health care coverage? (age 18+)';
	yearA[yearA.length]='1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topHCA; menuA[menuA.length]=''; valA[valA.length]='hlthpln_ok'; varA[varA.length]='Do you have any health care coverage? (age 18-64)';
	yearA[yearA.length]='1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'; 
topA[topA.length]=topHCA; menuA[menuA.length]=''; valA[valA.length]='persdoc_'; varA[varA.length]='Do you have a personal doctor? 18+';
	yearA[yearA.length]='2007,2008';
 
// THE WHOLE CODE SNIPPET INVOLVED
var catchanged=false; var catyearsdone=false; var currentCat2=''; var catHasChildren; 
var menuNums=1; var catAlreadyPrinted=true;
for (var x = 0; x < topA.length; x++) 
{
	if(x==0){currentCat2=topA[x]; catHasChildren=false;} //Sets things up on 1st row in array.
	
	if ((currentCat2 != topA[x]) && (x!=0))//Catches 1st instance of each cat & 
	{													//checks previous cat for children.	
		if(catHasChildren==false)
		{	/*Don't add cat since it has no children*/ }
		else
		{	/* Add last cat since it has children. */ 
 
					var debugvar = '"' + topA[x - 1] + '":"' + currentCat2 +'":'+''+'';
					
					
					//if(topA[x - 1] ==currentCat2())
					//{
						
						box1.options[menuNums-1] = new Option(debugvar, menuNums);					
						//box1.options[menuNums-1] = new Option(topA[x - 1], menuNums);
						menuNums++; //catHasChildren = false;
						catHasChildren=false;
						
					//}
		}
	}
	currentcat2=topA[x];
	var catyearsdone=false;
	for (var i = parseInt(startyearChoice, 10), n = parseInt(endyearChoice, 10); i <= n; i++) {
		if ((yearA[x].match(i)) && (!catyearsdone)) {
			//var msg=''; box2.options[menuNums]=new Option(varSubA[x]+ msg,valueSubA[x]); 
			catyearsdone = true; catHasChildren = true;
		}
	}				
}	

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:

Select allOpen in new window

 

by: LeprechaunPosted on 2009-06-10 at 13:12:03ID: 24595813

The array is a data source for some dynamic page elements where a subcategory list gets populated depending on the category selected by the user.

What I'm trying to do in this code is only display categories that have child subcategories that fall within a given date range.

Thanks.

 

by: LeprechaunPosted on 2009-06-10 at 14:24:34ID: 24596714

I tracked down the bug. It was "currentcat2" on line 92 above. Should be "currentCat2".

Haven't worked out exactly why the IF statement caused such a difference, but it's working now, so I'm back in business.  

I'm going to request that this question be deleted since it turned out to be so trivial. (Duh!)

Thanks.

 

by: HonorGodPosted on 2009-06-10 at 16:06:17ID: 24597389

Is this closer to what you wanted?

<html>
<body>
 
<script type="text/javascript">
//THE ARRAY POPULATING CODE INVOLVED
topDemo      = 'Demographics'
topGeo       = 'Geography'
topYear      = 'Year'
topHStat     = 'Health Status'
topHCA       = 'Health Care Access'
topDiab      = 'Diabetes'
topArth      = 'Arthritis'
topCardio    = 'Cardiovascular Disease'
topHyper     = 'Hypertension Awareness'
topAsthma    = 'Current Asthma'
topChol      = 'Cholesterol'
topWeight    = 'Weight Control'
topTobacco   = 'Tobacco Use'
topAlcohol   = 'Alcohol Consumption'
topOHealth   = 'Oral Health'
topImmuns    = 'Immunizations'
topWomHealth = 'Women Health'
topProsCanc  = 'Prostate Cancer Screening'
topColoCanc  = 'Colorectal Cancer Screening'
topFirearm   = 'Firearm'
topIPViol    = 'Intimate Partner Violence'
topSexViol   = 'Sexual Violence'
topFruitVeg  = 'Fruits and Vegetables'
topSleep     = 'Sleep'
topSBelt     = 'Seat Belt Use'
 
Array.prototype.append = function( item ) {
  this[ this.length ] = item
}
 
function years( first, last ) {
  var result = []
  for ( var y = first; y <= last; y++ ) {
    result.append( y )
  }
  return result.join( ',' )
}
 
topA  = []
varA  = []
yearA = []
valA  = []
menuA = []
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'SEX' )
varA.append( 'Gender of respondent' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'age_ok' )
varA.append( 'What is your age? (3 groups)' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'ageg_ok' )
varA.append( 'What is your age? (7 groups)' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'race_ok' )
varA.append( 'What is your race/ethnicity?' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'educa_ok' )
varA.append( 'What is the highest level of education completed?' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'income_ok' )
varA.append( 'What is your annual household income?' )
yearA.append( years( 1988, 1992 ) )
 
 
topA.append( topDemo )
menuA.append( '' )
valA.append( 'marital' )
varA.append( 'What is your marital status?' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topGeo )
menuA.append( '' )
valA.append( 'CTYCODE' )
varA.append( 'Residence (County)' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topGeo )
menuA.append( '' )
valA.append( 'Region' )
varA.append( 'Residence (Region)' )
yearA.append( years( 1988, 2008 ) )
 
topA.append( topYear )
menuA.append( '' )
valA.append( 'Year' )
varA.append( 'Year' )
yearA.append( years( 1988, 2007 ) ) //TEMP 2008
 
topA.append( topHStat )
menuA.append( '' )
valA.append( 'GENHLTH' )
varA.append( 'How is your general health? ( 2 groups)' )
yearA.append( years( 1993, 2008 ) )
 
topA.append( topHStat )
menuA.append( '' )
valA.append( 'PHYSHLTH' )
varA.append( 'Number of physically unhealthy days (last month)' )
yearA.append( years( 1993, 2008 ) )
 
topA.append( topHStat )
menuA.append( '' )
valA.append( 'MENTHLTH' )
varA.append( 'Number of mentally unhealthy days (last month)' )
yearA.append( years( 1993, 2008 ) )
 
topA.append( topHStat )
menuA.append( '' )
valA.append( 'poorh_ok' )
varA.append( 'Number of days poor health restrict activity (last month)' )
yearA.append( years( 1993, 2008 ) )
 
topA.append( topHCA )
menuA.append( '' )
valA.append( 'hlthpln' )
varA.append( 'Do you have any health care coverage? (age 18+)' )
yearA.append( years( 1991, 2008 ) )
 
topA.append( topHCA )
menuA.append( '' )
valA.append( 'hlthpln_ok' )
varA.append( 'Do you have any health care coverage? (age 18-64)' )
yearA.append( years( 1991, 2008 ) )
 
topA.append( topHCA )
menuA.append( '' )
valA.append( 'persdoc_' )
varA.append( 'Do you have a personal doctor? 18+' )
yearA.append( years( 2007, 2008 ) )
 
// THE WHOLE CODE SNIPPET INVOLVED
var catchanged   = false
var catyearsdone = false
var currentCat2  = ''
var catHasChildren
var menuNums     = 1
var catAlreadyPrinted = true
 
alert( topA.length )
 
for ( var x = 0; x < topA.length; x++ ) {
//alert( 'x: ' + x + ' kids: ' + catHasChildren + ' menuNums: ' + menuNums + ' Cat2: ' + currentCat2 )
  if ( x == 0 ) {
    currentCat2 = topA[ x ]
    catHasChildren = false
  } //Sets things up on 1st row in array.
 
  if ( ( currentCat2 != topA[ x ] ) && ( x != 0 ) ) {  //Catches 1st instance of each cat &
    if ( catHasChildren == false ) {                   /*Don't add cat since it has no children*/
    } else {                                           /* Add last cat since it has children. */
      var debugvar = '"' + topA[x - 1] + '":"' + currentCat2 +'":'+''+'';
 
      //if(topA[x - 1] ==currentCat2())
      //{
//      box1.options[menuNums-1] = new Option(debugvar, menuNums);
        document.write( debugvar + '<br>' )
        //box1.options[menuNums-1] = new Option(topA[x - 1], menuNums);
        menuNums++; //catHasChildren = false;
        catHasChildren=false;
      //}
    }
  }
 
  currentcat2 = topA[x];
  var catyearsdone = false;
  startyearChoice = yearA[ 0 ]
  endyearChoice   = yearA[ yearA.length - 1 ]
  for ( var i = parseInt(startyearChoice, 10), n = parseInt(endyearChoice, 10); i <= n; i++) {
    if ( ( yearA[ x ].match( i ) ) && ( !catyearsdone ) ) {
      //var msg=''; box2.options[menuNums]=new Option(varSubA[x]+ msg,valueSubA[x]);
      catyearsdone = true
      catHasChildren = true;
    }
  }
}
 
</script>
 
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:

Select allOpen in new window

 

by: HonorGodPosted on 2009-06-10 at 16:07:58ID: 24597400

Please don't delete it.  Please award me some points for the time and effort

 

by: LeprechaunPosted on 2009-06-11 at 06:04:01ID: 24601673

Sorry, was just thinking in terms of whether its "answer" (i.e., watch for typos) was useful, but I see what you're saying.

 

by: LeprechaunPosted on 2009-06-11 at 06:15:02ID: 24601800

Thanks for the clean-up, HonorGod. That is a lot nicer to look at. (Need to learn about this prototype stuff.)

That code's cross-browser compatible? I know some of the handier array functions aren't.

 

by: HonorGodPosted on 2009-06-11 at 06:29:29ID: 24601977

Yes, that is.

 

by: HonorGodPosted on 2009-06-11 at 06:29:55ID: 24601984

I tried it on IE8, FireFox & Chrome

 

by: LeprechaunPosted on 2009-06-11 at 06:54:58ID: 24602279

BTW, what's the advantage to having the years set up with that function?

Also, will the yearA[ x ].match( i ) function still work since the yearA field is no longer a simple text field?

Tx.

 

by: HonorGodPosted on 2009-06-11 at 07:06:53ID: 24602450

I just found it easier to read this:

years( 1988, 2008 )

to this:

'1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008';

So, it's a personal preference.  

Q: will the yearA[ x ].match( i ) function still work since the yearA field is no longer a simple text field?
A: But that's just it, it IS a simple text (string) field.  The result of this:

years( 1988, 2008 )

is identical to this:

'1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008'

Watch:

<html>
<body>
 
<script type="text/javascript">
 
Array.prototype.append = function( item ) {
  this[ this.length ] = item
}
 
function years( first, last ) {
  var result = []
  for ( var y = first; y <= last; y++ ) {
    result.append( y )
  }
  return result.join( ',' )
}
 
if ( years( 1988, 2008 ) == '1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008' ) {
  alert( "They're identical." )
} else {
  alert( "They're different." )
}
 
</script>
 
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:

Select allOpen in new window

 

by: LeprechaunPosted on 2009-06-11 at 07:28:47ID: 24602756

That's neat, but there's a small problem. Your code assumes that the numbers are part of continuous ranges, but actually some of questions skip years, which is why I'm working on this. (I need to enable or disable topics depending on the year the user has selected--we don't want a user submitting a report query that will come back empty because options are being shown that are irrelevant to the time range they've selected).

As ugly as it is, I figured this was the simplest way to handle all the possible permutations without creating parsing logic for the variant date info. This way, I can do a simple InString- style check for each year in the time period involved and don't have to parse multiple date ranges (e.g., "1988-2000, 2002, 2004-2006, 2008"). It's ugly, but so long as the years are correct in the array, it's straight forward parse.

If you have a smarter suggestion, I'm all ears, though. I don't like having all those years needlessly cluttering the place up, either.

Thanks.

 

by: HonorGodPosted on 2009-06-11 at 08:54:36ID: 24603812

How many years do you want to exclude?  A couple?  If so, this may be a good answer.  It allows exclusion years to be added to the parameter list.  So, to have a range of dates from

1988..2008 without 2001, you would use:

years( 1988, 2008, 2001 )

To be more complicated and specify 1988..2008, then exclude 1988, 2001, 2008, you would use:

years( 1988, 2008, 1988, 2001, 2008 )

of course, this would be better written as:  years( 1989, 2007, 2001 ), but it's a reasonable test

Does that make sense?

<html>
<body>
 
<script type="text/javascript">
if ( !Array.indexOf ) {
  Array.prototype.indexOf = function( val ) {
    for ( var i = this.length - 1; ( i > -1 ) && ( this[ i ] != val ); i-- ) {
    }
    return i
  }
}
 
Array.prototype.append = function( item ) {
  this[ this.length ] = item
}
 
function years( first, last ) {
  var result = []
  for ( var y = first; y <= last; y++ ) {
    result.append( y )
  }
  for ( var i = 2; i < arguments.length; i++ ) {
    y = arguments[ i ]
    var x = result.indexOf( y )
    if ( x > -1 ) {
      if ( x > 0 ) {
        if ( x < result.length - 1 ) {
//        alert( 'Exclude: ' + y )
          result = result.slice( 0, x ).concat( result.slice( x + 1 ) )
        } else {
//        alert( 'Exclude last year: ' + y )
          result.length--
        }
      } else {
//      alert( 'Exclude first year: ' + y )
        result = result.slice( 1 )
      }
    } else {
      alert( 'Not found: ' + y )
    }
  }
  return result.join( ',' )
}
 
document.write( '1989..2007 - 2001: ' + years( 1988, 2008, 1988, 2001, 2008 ) + '<br>' )
document.write( '1988..2008 - 2000..2002: ' + years( 1988, 2008, 2000, 2001, 2002 ) + '<br>' )
 
</script>
 
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:

Select allOpen in new window

 

by: LeprechaunPosted on 2009-06-11 at 08:58:19ID: 31591037

This is very nice. Thank you.

 

by: LeprechaunPosted on 2009-06-11 at 09:00:50ID: 24603881

One question about the new years function: Wouldn't it be easier both from a coding standpoint and a data maintenance standpoint for the base range to be separated from exceptions by something other than a ',' ?  

Thanks.

 

by: LeprechaunPosted on 2009-06-11 at 09:03:03ID: 24603912

Any reason no to do it like this? "years( 1988-2008;1988, 2001, 2008 )" That seems less prone to data entry error, I think.

 

by: HonorGodPosted on 2009-06-11 at 09:26:12ID: 24604133

Well, the script could be re-written to parse a string I suppose.  So, that would have to look like:

years( '1988-2008;1988, 2001, 2008' )

or perhaps

years( '1988-2008', 1988, 2001, 2008 )

The choice of a comma separator is because commas are used to separate parameters in a function.

Thanks for the grade & points.

Good luck & have a great day.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...