• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

Javascript case statement : maximum allowed?

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
stevie21
Asked:
stevie21
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the number of case statements is only limited by the data type of the expression, in this case (n).
0
 
nizsmoDeveloperCommented:
i wouldn't think there is a limit, just how many you can fit in :)
0
 
pavaneeshkumarCommented:
agree with nizsmo
there is no restrictions on no of case statements u put in switch.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Michel PlungjanIT ExpertCommented:
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
 
stevie21Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
stevie21Author Commented:
Why is that better than case/switch?
0
 
Michel PlungjanIT ExpertCommented:
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
 
stevie21Author Commented:
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
 
stevie21Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now