1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

JS makes me angry - if statement, & operator

Discussion in 'JavaScript' started by Icanthackthis, Jul 16, 2018.

  1. #1
    Hi guys!

    I have this simple code:

    
    //$UT = parseInt(document.getElementById("XML_type").value);
    //$ASZ = document.getElementById("XML_person").value;
    $UT = 1;
    $ASZ = "0123456789101";
    
    $LE= $ASZ.length;
                                 
    if(($UT&2 == 2 && $LE != 13) || ($UT&2 != 2 && $LE> 0)){
       msg = "ALERT";
       console.log(msg);
    }
    
    Code (markup):
    I just simply cant understand what's going on...
    If $LE= 13 there is no way to go to the statement. I think i strongly misunderstand something :(
    Help plz!
     
    Icanthackthis, Jul 16, 2018 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    What is the $UT&2 meant to be checking? Looks like string concatenation but then comparing to an integer

    I had a mess about changing the & to a +

    var UT = 1;
    var ASZ = "0123456789101";
    //var ASZ = "012345678910";
    var LE = ASZ.length;
    
    console.log(UT & 2);
    console.log(LE);
    
    if ((UT + 2 == 2 && LE != 13) || (UT + 2 != 2 && LE > 0)) {
      msg = "ALERT";
      console.log(msg);
    }
    
    if (UT + 2 == 2 && LE != 13) {
      msg = "ALERT1";
      console.log(msg);
    }
    else if(UT + 2 != 2 && LE > 0) {
      msg = "ALERT2";
      console.log(msg);
    }
    
    Code (JavaScript):
     
    sarahk, Jul 16, 2018 IP
  3. Icanthackthis

    Icanthackthis Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    Thanks for your reply!
    I got it!

    The correct way to put the & operator into '()', as both are logical expressions, and == has the priority over the &. Grrr

    
    //$UT = parseInt(document.getElementById("XML_type").value);
    //$ASZ = document.getElementById("XML_person").value;
    $UT = 1;
    $ASZ = "0123456789101";
    
    $LE= $ASZ.length;
    
    if((($UT&2) == 2 && $LE != 13) || (($UT&2) != 2 && $LE> 0)){
    msg = "ALERT";
    console.log(msg);
    }
    
    Code (markup):
     
    Icanthackthis, Jul 16, 2018 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    Since you're only evaluating binary and, you don't need to == 2 in the first bloody place. You're only checking one binary bit, so it's either going to be 0 or 2. With zero being boolean false, you could simply have written that as:

    
    if (
    	(($UT & 2) && $LE != 13) ||
    	(!($UT & 2) && $LE > 0)
    ) {
    
    Code (markup):
    Since we have loose boolean, use it. Same as doing a "test" in assembler, jz or jnz.

    Also if LE would never be negative, you could ditch the > 0 for just && $LE

    Though this isn't PHP, what's with all the dollar sign prefixing?
     
    Last edited: Jul 19, 2018
    deathshadow, Jul 19, 2018 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    I feel better now for not knowing either!
     
    sarahk, Jul 19, 2018 IP