Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

4 arrays 5 options = 1024 IF statements......no cool. 2

Status
Not open for further replies.

nochoice

Programmer
Jun 17, 2003
72
0
0
CA
I have, not necessarily a Jscript question, but a programing question.

In an html file I call a Jscript function and pass it 4 variables:

<script language=&quot;JavaScript1.2&quot;>myfunction(var1,var2,var3,var4);</script>

the Jscript function is in an external .js file.

Each variable can have up to 5 options opt1, opt2, opt3, opt4, opt5 - for simplicity.

the 4 variables correspond to 4 components of output to the screen: var1 is email address, var2 is number of times visited site, etc......

so my output:

if (var1=opt1 and var2=opt2 and var3=opt3).......
document.write(opt1[count]+&quot; &quot;+opt2[count]+&quot; &quot;+etc....);

elseif (var1=opt2 and var2=opt2 and var3=opt3).......
document.write(opt2[count]+&quot; &quot;+opt2[count]+&quot; &quot;+etc....);

can you see where this is going? One thousand and Twenty Four IF statements, that's not cool at all........

let me try to clarify some questions you might have:

1) the options are arrays defined in the external .js file. So the email address opt1 will have say 27 email addresses in the array.
2) this is for survey results
3) no I can't use php
4) it's for sorting purposes that are relatively irrelevant to this question
5) I asked in this forum because I was hoping Jscript has some fancy something I can do to eliminate the need for so many IF statements.

I am very openminded and will try just about anything,
Thanks for your help,
Michael

 
you might be able to make some helper functions, but it's a little too abstract to give an example yet.

can you provide a real example of what each of the five &quot;opt&quot;s can be for each of the four &quot;var&quot;s ? this might help clarify what you're attempting


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
it's a bit abstract for me too I haven't written the code yet I'm working on structure right now, but here's what I can tell you:

var1 = what should be displayed in the first column
var2 = what should be displayed in the second column
var3 = what should be displayed in the third column
var4 = what should be displayed in the fourth column


opt1 = email - if available, some people don't want this plastered all over the web.
opt2 = times visited site - based on cookies
opt3 = section you like the most
opt4 = section you dislike the most
opt5 = what you would change
opt6 = blank - oops forgot about this one - 6 options

my output:

document.write(var1+&quot;|&quot;+var2+&quot;|&quot;+var3+&quot;|&quot;+var4);

So, the user fills in 4 dropdown boxes - Which order would you like the survey information to be displayed in. each box having 6 options.

then a CGI script generates, and displays, 2 lines of Jscript. These 2 lines of Jscript can be entered into any website. This Jscript is what calls myfunction(), here are the 2 lines:

<script language=&quot;JavaScript1.2&quot;
src=&quot;</script>

<script language=&quot;JavaScript1.2&quot;>myfunction
(&quot;e_address&quot;,&quot;blank&quot;,&quot;times_v&quot;,&quot;most_like&quot;);
</script>

I pass the variables to the function as strings, I can use anything really.

Now, when the user puts the 2 lines of Jscript on their website, they get the survey results - which are constantly updated, which is why I use a script instead of hardcoding - obviously.

did this help out?
I hope I answerd your question, if not ask more....
 
how about something like this? (lots of pseudo-code, but you should get the idea)
Code:
function showSurvey(col1, col2, col3, col4) {
	var output = [];
	var pipe = &quot;&quot;;
	
	//  loop through the four arguments
	for (var x = 0; x < arguments.length; x++) {
		output.push( pipe + getCol(arguments[x]) );
		pipe = &quot; | &quot;;
	}
	
	return output.join(&quot;&quot;);
}

function getCol(sName) {
	switch(sName) {
		case &quot;e_address&quot;:
			return getEmailAddress();
			break;
		case &quot;blank&quot;:
			return &quot; &quot;;
			break;
		case &quot;times_v&quot;:
			return getTimesVisited();
			break;
		case &quot;most_like&quot;:
			return getMostLike();
			break;
	}
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Ahhh...

You need something like this:

Code:
   // This is the data object;
   var surveyData = new Array(
         { &quot;email&quot;: &quot;a@b&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;b@c&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;c@d&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;a@b&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      ,  { &quot;email&quot;: &quot;x@yb&quot;, &quot;visted&quot;: 10, &quot;liked&quot;: &quot;pictures of burgers&quot;, &quot;disliked&quot;: &quot;sums&quot;, &quot;change&quot;: &quot;name of site&quot; }
      // ... etc
   );
   // now display the data;
   function myfunction(var1, var2, var3, var4) {
      var disp = &quot;&quot;;
      var vArray = new Array(var1, var2, var3, var4);
      var j = 0;
      var k = 0;
      for (j = 0; (j < surveyData.length); ++j) {
         disp = &quot;&quot;;
         for (k = 0; (k < surveyData.length); ++k) {
            // get the field value (vArray contains the names);
            var v = surveyData[j][vArray[j]];            
            // if the field name is not defined, we'll treat it as blank
            if (v == null) {
               disp += &quot; - &quot;; // use a dash for blanks, for example
            } else {
               disp += v + &quot; &quot;;
            }
         }
         document.write(disp + &quot;\n&quot;);      
      }
      return;
   }

Hope this helps.


[tt]________________________________________________________________
[pc2]Roger
Life is a game of cards in which the deck contains only jokers.[/tt]
 
Wow! thanks,

I think it's time to do some coding. I'll let you know how it turns out.
 
Awesome, It works perfectly!

I combined both of your ideas and came up with this:


output = new Array(first, second, third);

//define arrays

for(count=0; count < url.length; count++) {

document.write (url[count]);

for (counter=0; counter < output.length; counter++) {

if (output[counter] == &quot;times_v&quot;){
document.write(times_v[count] + &quot;|&quot;);
}
if (output[counter] == &quot;e_address&quot;){
document.write(e_address[count] + &quot;|&quot;);
}
if (output[counter] == &quot;blank&quot;){
document.write(&quot; - &quot; + &quot;|&quot;);
}

}

document.write (&quot;</a> <br> \n&quot;);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top