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.
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.
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.
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.
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);
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.
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.
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;
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.