stevie21
asked on
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
}
??
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
}
??
the number of case statements is only limited by the data type of the expression, in this case (n).
i wouldn't think there is a limit, just how many you can fit in :)
agree with nizsmo
there is no restrictions on no of case statements u put in switch.
there is no restrictions on no of case statements u put in switch.
But show us what the tests are and perhaps we can change the code to not use switch which I personally find very ugly
ASKER
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.
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:
//
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Why is that better than case/switch?
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
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
ASKER
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!!
I need that but I'm not certain if I can do that with the array. Can I?
Thanks for the continued help!!
ASKER
document.lvar.elements["va riable"+(i +1)].value =(texts)?t exts[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"];
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"];
try it and see
document.lvar.elements["va riable"+(i +1)].value =(texts)?t exts[i]:"n /a";
will show n/a if the text is empty, null or 0
document.lvar.elements["va
will show n/a if the text is empty, null or 0