Solved

Javascript case statement : maximum allowed?

Posted on 2007-11-18
11
277 Views
Last Modified: 2008-02-01
I have a page which can only be html rather than aspx/php and in there is a bit of javascript and a case statement.
What is the maximum number of "cases" I can have in here?

i.e.

switch(n)
{
case 1:
  execute code block 1
  break    
case 2:
  execute code block 2
  break
default:
  code to be executed if n is
  different from case 1 and 2
}


??
0
Comment
Question by:stevie21
11 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20307099
the number of case statements is only limited by the data type of the expression, in this case (n).
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20307100
i wouldn't think there is a limit, just how many you can fit in :)
0
 
LVL 5

Expert Comment

by:pavaneeshkumar
ID: 20307122
agree with nizsmo
there is no restrictions on no of case statements u put in switch.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20307204
But show us what the tests are and perhaps we can change the code to not use switch which I personally find very ugly
0
 

Author Comment

by:stevie21
ID: 20307604
I have a querystring which is dealt with by the javascript, and 7 or 8 text fields in the html page which change (and are populated by the javascript) depending on the querystring value.
The querystring's always a 3 digit number & the page that reads it shows a what variables to put into a letter that I would send out.  The letter might say, for example,
"Dear customer, you asked for more info on the following product :
<insert variable 1>
The price is <variable 2> and we have <variable 3> of them in stock" etc.
and these blanks are filled in by javascript.

A different letter might say
"Here's your invoice :
You owe us <variable 1> and this is due on <variable 2> " etc.

The point of the page is the if the querystring is 001 then there's a table listing what values to put into each variable.  In the first text field, it's say "product name" and variable 2 = "price" etc.
If it's letter 002 then variable 1 = "amount owed" and 2 = due date.

You've answered my question and I think I've explained teh above really badly but if there's a better way to do this (other than endless if statements) then feel free.
function ltrFunc()

{

var ltr = querySt("ltr");

	document.lvar.variable1.value = "n/a";

	document.lvar.variable2.value = "n/a";

	document.lvar.variable3.value = "n/a";

	document.lvar.variable4.value = "n/a";

	document.lvar.variable5.value = "n/a";

	document.lvar.variable6.value = "n/a";

	document.lvar.variable7.value = "n/a";

	document.lvar.variable8.value = "n/a";

switch(ltr)

{

case "132":

	document.lvar.variable1.value = "Transaction Amount 1";

	document.lvar.variable2.value = "Transaction Amount 2";

	document.lvar.variable3.value = "Transaction Date 1";

	document.lvar.variable4.value = "Transaction Date 2";

	document.lvar.variable5.value = "Transaction Date 3";

	document.lvar.variable6.value = "Merchant Name";

	document.lvar.variable7.value = "Transaction Amount 3";

  break

case "447":

	document.lvar.variable6.value = "Product name";

  break

default:

//

}

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 50 total points
ID: 20307848
How about

var myvars = new Array();

myVars["v_132"]=[

"Transaction Amount 1",

"Transaction Amount 2",

"Transaction Date 1",

"Transaction Date 2",

"Transaction Date 3",

"Merchant Name",

"Transaction Amount 3"];

 

myVars["v_447"]=[

"Product Name",

"bla",

"bla",

"bla",

"bla",

"bla",

"bla"];

 

// and use

 

var ltr = querySt("ltr");

function ltrFunc() {

  var texts = myVars[ltr];

  for (var i=0;i<7;i++) {

    document.lvar.elements["variable"+(i+1)].value=(texts)?texts[i]:"n/a"; 

  }

}

Open in new window

0
 

Author Comment

by:stevie21
ID: 20315839
Why is that better than case/switch?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20318572
because with switch,
1. the script will scan all the way down the code to find the item
2. when you add an item, you need to edit executable code and can break it (without getting an error) with a missplaced or missing break whereas my suggestion you need only to edit the array entries, if you break that, you get an error

Also my suggestion lends itself to more clever formatting like JSON or object notation which will make it even smaller.

You can even reuse the strings:

myVars["v_132"][3] = myVars["v_135"][3]

for example
0
 

Author Comment

by:stevie21
ID: 20320426
With the switch, if I don't declare what value variable1 (or 2 or 3 etc.) is then it's automatically displayed on the screen as "n/a".
I need that but I'm not certain if I can do that with the array.  Can I?

Thanks for the continued help!!
0
 

Author Comment

by:stevie21
ID: 20320464
document.lvar.elements["variable"+(i+1)].value=(texts)?texts[i]:"n/a";

In my example, ltr447 only has variable 6.  1-5 need to show as "n/a" as do 7 and above.

Would I declare that in the array as
myVars["v_447"]=[
"",
"",
"",
"",
"",
"Product Name"];
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20321003
try it and see

 document.lvar.elements["variable"+(i+1)].value=(texts)?texts[i]:"n/a";

will show n/a if the text is empty, null or 0
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now