Solved

Javascript case statement : maximum allowed?

Posted on 2007-11-18
11
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 143

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

696 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