Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Is it possible to pinpoint the condition met in "if(...||...||...)"

    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?

  • #2
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,700
    Thanks
    0
    Thanked 233 Times in 228 Posts
    Hi there CrackTheAir,

    how about like this...
    Code:
    
    if(x==1||x==2||x==3||x==4){
       alert('TEXT');
       alert(x);
     }
    else {
       alert(x);
     }
    
    coothead

  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    Fabric Covered Box
    Posts
    69
    Thanks
    1
    Thanked 16 Times in 14 Posts
    Code:
    var c=0;
    if(++c && x=='one' || ++c && x=='two' || ++c && x=='three') {
      alert('condition '+c+' was met.');
    }
    else{
      alert('FAIL!!1!11!');
    }

  • Users who have thanked Shannon Blonk for this post:

    Old Pedant (06-03-2009)

  • #4
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Shannon Blonk View Post
    Code:
    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.

  • #5
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

    Code:
    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
    Last edited by CrackTheAir; 06-03-2009 at 01:22 AM. Reason: Reword

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Using Shannon's very clever idea with a very minor mod:
    Code:
    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?
    Code:
    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:
    Code:
    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";

  • #7
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Shannon Blonk View Post
    Code:
    var c=0;
    if(++c && x=='one' || ++c && x=='two' || ++c && x=='three') {
      alert('condition '+c+' was met.');
    }
    else{
      alert('FAIL!!1!11!');
    }
    Quote Originally Posted by CrackTheAir View Post
    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?

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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.

  • #9
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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:

    Code:
    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');
    }

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    adios wrote:
    No.
    Okay, sayonara, is that just being sardonic or do you disagree with all the solutions we've come up with??

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Ooh ooh ooh ooh!

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

    So one more!
    Code:
    <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>

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Deacon: Yes, you read it exactly correctly.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,518
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    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."/>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •