Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Conditionals in switch 'case' labels

Reply
Thread Tools

Conditionals in switch 'case' labels

 
 
Mark Anderson
Guest
Posts: n/a
 
      02-26-2004
Is this sort of thing possible:

var X = 'Moe';
switch (X) {
case 'Curly'||'Moe'||'Larry':
alert('Found one of the Three Stooges');
case 'Chico'||'Harpo'||'Zeppo'||'Grouco'||'Gummo':
alert('Found one of the Marx Brothers');
default:
alert('No matches');

This gives 'No matches' unless I only put a single string in the 'case'
lines. I've just been using VB's Select Case which is a similar flow
control but which allows conditional arguments in the 'cases'. I just
wondered...

I realise you could put each set of names in an array and iterate
through each array, but that's a different issue.

Regards

Mark Anderson


 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      02-26-2004
> Is this sort of thing possible:
>
> var X = 'Moe';
> switch (X) {
> case 'Curly'||'Moe'||'Larry':
> alert('Found one of the Three Stooges');
> case 'Chico'||'Harpo'||'Zeppo'||'Grouco'||'Gummo':
> alert('Found one of the Marx Brothers');
> default:
> alert('No matches');
>
> This gives 'No matches' unless I only put a single string in the 'case'
> lines. I've just been using VB's Select Case which is a similar flow
> control but which allows conditional arguments in the 'cases'. I just
> wondered...
>
> I realise you could put each set of names in an array and iterate
> through each array, but that's a different issue.


var X = 'Moe';
switch (X) {
case 'Curly':
case 'Moe':
case 'Larry':
alert('Found one of the Three Stooges');
break;
case 'Chico':
case 'Harpo':
case 'Zeppo':
case 'Groucho':
case 'Gummo':
alert('Found one of the Marx Brothers');
break;
default:
alert('No matches');
}

You could also put them in an object:

var name = {
Curly: 'Three Stooges',
Moe: 'Three Stooges',
Larry: 'Three Stooges',
Chico: 'Marx Brothers',
Harpo: 'Marx Brothers',
Zeppo: 'Marx Brothers',
Chico: 'Marx Brothers',
Groucho: 'Marx Brothers'}

var n = name[X];
if (n) {
alert('Found one of the ' + n);
} else {
alert('No matches');
}

JavaScript is not VBScript. You need to better educate yourself.

http://www.crockford.com/#javascript
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      02-26-2004
"Mark Anderson" <(E-Mail Removed)> writes:

> Is this sort of thing possible:
>
> var X = 'Moe';
> switch (X) {
> case 'Curly'||'Moe'||'Larry':


The syntax is the traditional C syntax, so you can write:
case 'Curly': case 'Moe': case 'Larry':
alert('Found one of the Three Stooges');
break;

Remember the break, or the following code will also be executed.

> This gives 'No matches' unless I only put a single string in the 'case'
> lines.


It would actually match the first option. Try with 'Curly'.
The reason is that Javascript allows expressions in the case construct.
It first evaluates
'Curly'||'Moe'||'Larry'
The value of that is 'Curly'. I then tests that value against the
switch value, so 'Curly' would be matched.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Mark Anderson
Guest
Posts: n/a
 
      02-26-2004
Douglas,

Thanks very much.

>> JavaScript is not VBScript. You need to better educate yourself.


Ouch - I'm not sure what I said to warrant such a put down. Seeing as
this fact isn't in the FAQ and not in the basic 'switch' usage examples
I found online, is the question not valid in this newsgroup without
attracting such contempt?

FWIW, Visual Basic - to which I referred - is *not* VBScript. Indeed, it
is a pale shadow of the former but I'm here to say thanks for the info
supplied not for a newsgroup fight; I simply wondered whether a useful
method available in language A had similar offering in language B - I
wasn't implying, lest I was misunderstood, any comparative value
judgement. As it happens, my train of thought started was because I was
working in AppleScript (if I can mention that name here <g>) and wanted
a 'switch' type construct - which, as it happens, AS doesn't have.
Chopping between languages it's hard to remember the constructs of each
one - they tend to blur.

On a more positive note, thank you kindly for your examples. The object
approach is new to me and one I'll certainly look into further.

Regards

Mark


 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      02-26-2004
>>JavaScript is not VBScript. You need to better educate yourself.

> Ouch - I'm not sure what I said to warrant such a put down. Seeing as
> this fact isn't in the FAQ and not in the basic 'switch' usage examples
> I found online, is the question not valid in this newsgroup without
> attracting such contempt?


It was not contempt. It was good advice. In my opinion, browser
scripting is the most difficult form of programming because of the great
need for portability and the extremely poor quality and inconsistency of
the browsers. Rigor and knowledge are extremely important here.

You should not attempt to program in any language without a good
reference at hand.

http://www.crockford.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"pointlabel"-like function for Python: distribute text labels on a2-d scatter plot to avoid overlapping labels C Barrington-Leigh Python 1 09-12-2010 09:58 PM
C# to VB.net conversion for "?" conditionals .Net Sports ASP .Net 15 02-03-2006 04:11 PM
XSD: ways to deal with conditionals on attribute value? Piper707@hotmail.com XML 1 12-30-2005 09:10 PM
goto to switch labels Christopher Benson-Manica C Programming 7 10-06-2004 01:14 AM
Conditionals & paths Mustafa Aydin XML 0 04-21-2004 05:25 PM



Advertisments