help me understand this line of code

Discussion in 'JavaScript' started by PhilGlass, Feb 26, 2008.

  1. #1
    ok so i was given an task by my boss, get this calculator off this site and us it. well i'm trying ot understand it but i'm a php guy and not very good at it. javascript is not my cup of tea because well i'm just not there yet.

    anyways here's my problem
    
    if (calcWidth % 2) { 
    	var calcWidth = ++calcWidth;
    }
    
    Code (markup):
    ok so i need help figuring this out.
    I know that calcwidth is the variable and but what does % 2 mean?
    and wouldn't ++ just and 1 to the variable calcwidth? i hope this makes sense if you need anymore please just tell me and i'll post the whole kit and kabodle.
    ok thanks for your time.

    later days,
    phil.
     
    PhilGlass, Feb 26, 2008 IP
  2. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The is the modulus operator. The modulus gives the remainder of one integer divided by another. if (calcWidth % 2) { checks to see if there is a remainder. This type of code is usually done within a loop to alternate the display of something in a list. For example to alternate background colors. And yes ++calcWidth adds one to calcWidth. It kind of doesn't make sense to declare the variable, assign to the variable and increment the variable all in one statement. But maybe in context it makes more sense.
     
    LogicFlux, Feb 26, 2008 IP
  3. PhilGlass

    PhilGlass Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok so i want to understand fully, so in this line: if (calcWidth % 2)
    he is asking in lamensterms - if calcwidths remainder is 2 then and add 1 to calcwidth? is that right?

    thanks for awnsering
    phil.
     
    PhilGlass, Feb 27, 2008 IP
  4. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No, the statement is evaluating the result of calcWidth % 2 .

    if (calcWidth % 2)

    is equivilint to

    if ( (calcWidth % 2) != 0 )


    and javascript also evaluates 0 as being false. So the if statement in question is basically asking if calcWidth % 2 is true, or if it has a remainder, because if it doesn't have a remainder it will be 0, which will be evaluated as false.
     
    LogicFlux, Feb 27, 2008 IP
  5. PhilGlass

    PhilGlass Guest

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I appreciate your help thank you for helping me understand.

    later days
    phil.
     
    PhilGlass, Feb 27, 2008 IP
  6. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    If the (rather bizarre) statement var calcWidth = ++calcWidth; is the only thing inside the if block, you can collapse the entire thing into a single statement which ensures that the number is even (has no remainder):

    calcWidth += (calcWidth % 2);

    Or a slightly faster version would be:

    calcWidth += (calcWidth & 1);
     
    vpguy, Feb 28, 2008 IP
  7. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That's a rather odd piece of code. :D
     
    LogicFlux, Feb 28, 2008 IP
  8. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hey, thanks! :) I'm a rather odd fellow. :cool:
     
    vpguy, Feb 29, 2008 IP
  9. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    What the code is actually doing is making sure that calcWidth is an even number. If it isn't it adds one to it, making it even.
     
    lephron, Mar 2, 2008 IP
  10. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #10
    True, that's why I think vpguy's "calcWidth += (calcWidth & 1);" is particularly clever. Because it returns 1, which adds one, if the first bit in calcWidth is set, because if the first bit is set, it's an odd number. And it's probably faster because it's a bitwise operation.
     
    LogicFlux, Mar 2, 2008 IP
    vpguy likes this.
  11. vpguy

    vpguy Guest

    Messages:
    275
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Anytime you need to use the modulus function (x % y) you can get the same result by using (x & (y-1)). A bitwise AND operation is significantly faster than a division operation, although since the language in question is Javascript, I'm not sure if it makes all that much of a difference.

    My favorite bit-twiddling hack is the XOR integer swap without using a temporary variable:

    x ^= y;
    y ^= x;
    x ^= y;

    :)
     
    vpguy, Mar 2, 2008 IP
  12. LogicFlux

    LogicFlux Peon

    Messages:
    2,925
    Likes Received:
    102
    Best Answers:
    0
    Trophy Points:
    0
    #12
    AW! I spent like ten minutes looking at that code trying to figure out how it worked and finally reasoned it was just checking for even/oddness. :( When it comes to bitwise stuff, anything beyond using bitmasks for setting values and I start getting a headache.
     
    LogicFlux, Mar 2, 2008 IP