...

View Full Version : Is it possible to pinpoint the condition met in "if(...||...||...)"



CrackTheAir
06-02-2009, 09:18 PM
Hi all,

I was wondering if it was possible, say, that you have an if(...||...||...), is it possible to tell which specific condition was met?

For example,


if (x==1 || x==2 || x==3 || x==4){
alert("TEXT")
}


If you do happen to get the "TEXT" alert, is it possible to tell which condition was met?

coothead
06-02-2009, 09:44 PM
Hi there CrackTheAir,

how about like this...


if(x==1||x==2||x==3||x==4){
alert('TEXT');
alert(x);
}
else {
alert(x);
}

coothead

Shannon Blonk
06-02-2009, 09:52 PM
var c=0;
if(++c && x=='one' || ++c && x=='two' || ++c && x=='three') {
alert('condition '+c+' was met.');
}
else{
alert('FAIL!!1!11!');
}:)

Deacon Frost
06-02-2009, 09:55 PM
var c=0;
if(++c && x=='one' || ++c && x=='two' || ++c && x=='three') {
alert('condition '+c+' was met.');
}
else{
alert('FAIL!!1!11!');
}:)

I like that. A very interesting concept.

CrackTheAir
06-03-2009, 01:18 AM
Coothead,

Thank you for your reply, but what I really want is for a way for the program to know which condition was met, not me. For example, what if I wanted to write it inside of a div like this?



if (a==b||c==d){
document.getElementById("id").innerHTML=


What would I put in the innerHTML that would tell me which condition was met?

Shannon Blonk,

=\ lol, i don't get it

Old Pedant
06-03-2009, 02:25 AM
Using Shannon's very clever idea with a very minor mod:


var choices = ["Looks like a==b was true", "Yep, c was equal to d","So e really equals f?"];

var which = 0;

if ( a==b || ++which && c==d || ++which && e==f )
{
document.getElementById("id").innerHTML = choices[which];
}

Now, quite frankly, I think that is somewhat obscure coding and as clever as it is I probably wouldn't use it.

Why not play it straightforward?


var html = null;
if ( a == b )
{
html = "Looks like a==b was true";
} else if ( c == d ) {
html = "Yep, c was equal to d";
} else if ( e == f ) {
html = "So e really is same as f?";
}
if ( html != null ) document.getElementById("foobar").innerHTML = html;

You *could* do it with the trinary operator, also, but again that just seems to obscure what is going on. K.I.S.S.

What the heck...here's the trinary operator version, if you are a glutton for punishment:


document.getElementById("zamboni").innerHTML
= a==b ? "Looks like a==b was true"
: c==d ? "Yep, c was equal to d"
: e==f ? "So e really is same as f?"
: "none of the above";

Deacon Frost
06-03-2009, 02:34 AM
var c=0;
if(++c && x=='one' || ++c && x=='two' || ++c && x=='three') {
alert('condition '+c+' was met.');
}
else{
alert('FAIL!!1!11!');
}:)



Shannon Blonk,

=\ lol, i don't get it

Ok, variable c = 0. If variable x=='one', then c will increment, only once, because the conditional value has already been met. If x =='two', c will increment twice because of the first condition, and the second condition. The code will execute, eitherway. Etc.

Once the condition is found, c's value will determine which condition was met. Either the first, second, or third.

At least... that's how I read it? Am I wrong?

adios
06-03-2009, 03:44 AM
Hi all,

I was wondering if it was possible, say, that you have an if(...||...||...), is it possible to tell which specific condition was met?

For example,
Code:

if (x==1 || x==2 || x==3 || x==4){
alert("TEXT")
}

If you do happen to get the "TEXT" alert, is it possible to tell which condition was met?

No.

venegal
06-03-2009, 04:05 AM
Oh, fun, we are doing clever and obscure stuff you can't do in production! Count me in!

Suppose you want to know all the conditions that matched. Shannon Blonk's code only gives you the first condition met; if you want all of them you could (but should not!) use the following:


var x = 'one';
var y = 'two';
var z = 'not three';
var u = 'four';
var v = 'not five';

if(c = (x == 'one') | (y == 'two')<<1 | (z == 'three')<<2 | (u == 'four')<<3 | (v == 'five')<<4) {
for (i = 0; i <= Math.log(c)/Math.LN2; i++) {
(c & 1<<i) && alert('condition ' + (i + 1) + ' was met.');
}
} else{
alert('no match');
}

Old Pedant
06-03-2009, 05:06 AM
adios wrote:
No.

Okay, sayonara, is that just being sardonic or do you disagree with all the solutions we've come up with??

Old Pedant
06-03-2009, 05:22 AM
Ooh ooh ooh ooh!

I really hate Venegal's solution! So much that I love it!

So one more!


<script>
// change these to play with it, of course
var x=1;
var y=0;
var z=3;
// needed initialization
var html = "";
// voila!
if ( ( x==1 && (html+=',first') != null )
| ( y==2 && (html+=",second") != null )
| ( z==3 && (html+=",third") != null )
)
{
document.getElementById("zamboni").innerHTML = html.substring(1);
}
</script>

Old Pedant
06-03-2009, 05:24 AM
Deacon: Yes, you read it exactly correctly.

Old Pedant
06-03-2009, 05:25 AM
Venegal wrote:
Oh, fun, we are doing clever and obscure stuff you can't do in production!

Hey, wait a minute! Have you ever looked at, say, the JQuery library??? What do you mean, "can't do in production"??? If that crap isn't both clever and obscure, what is? <grin style="s.e."/>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum