views:

632

answers:

6

How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.

+14  A: 

Use modulus:

// Will evaluate to true if the variable is divisible by 2
variable % 2 === 0  
Andy E
love it! simple, easy, and you didn't make me feel like an idiot for asking the question...which (for all the others who don't already know) is a big deal to new jQuery/Javascript developers.
sadmicrowave
@sadmicrowave: we all start out as beginners at some point. The real problem is the lack of informative tutorials out there. The tutorials for absolute beginners should clarify the difference between JavaScript and jQuery and they just dive straight into the jQuery stuff.
Andy E
The first code I ever wrote was in JavaScript, and that was a "welcome" alert box popup. The year was 1997 and it was for my Geocities homepage. We definitely all start somewhere (some formal training helps, too, though).
Mike Atlas
@Mike Atlas: The earliest piece I can remember, I wrote a MIDI file music player for Internet Explorer with a select box for the music file. The good ol' days, eh?
Andy E
@et al - thanks for the confidence boost guys. This forum really helps alot -- easily the best one I've found
sadmicrowave
+5  A: 

You don't need jQuery. Just use JavaScript's Modulo operator.

Mike Atlas
+2  A: 

You can use the modulus operator like this, no need for jQuery. Just replace the alerts with your code.

var x = 2;
if (x % 2 == 0)
{
  alert('even');
}
else
{
  alert('odd')
}
wsanville
+1  A: 
var x = 2;
x % 2 ? oddFunction() : evenFunction();
Pablo Cabrera
+1  A: 

You can also:

if (x & 1)
 itsOdd();
else
 itsEven();
Alex K.
+1 - I do like this answer, but you should be wary of [using bitwise operators unnecessarily](http://www.jslint.com/lint.html#bitwise) in JS.
Andy E
+5  A: 

Seriously, there's no jQuery plugin for odd/even checks?

Well, not anymore - releasing "Oven" a jQuery plugin under the MIT license to test if a given number is Odd/eVEN.

Source code is also available at http://jsfiddle.net/HRBPX/

Test-suites are available at http://jsfiddle.net/2XjBk/

(function() {
    /*
     * isEven(n)
     * @args number n
     * @return boolean returns whether the given number is even
     */
    jQuery.isEven = function(number) {
        return number % 2 == 0;
    };

    /* isOdd(n)
     * @args number n
     * @return boolean returns whether the given number is odd
     */
    jQuery.isOdd = function(number) {
        return !jQuery.isEven(number);
    };
})();​
Anurag
+1 for the joke... at least I hope you're joking ;-)
Andy E
Wow. Funny, but you do realise someone will actually use this?
Tim Down
@Tim Down: I wonder if anyone uses that [jQuery basic arithmetic plugin](http://www.reddit.com/r/programming/comments/buosj/add_a_number_to_another_number_in_javascript_img/c0on5ev)? Next on the list, a jQuery random number generator ;-)
Andy E
There is so much that jQuery can do, like creating variables, functions, etc., that is still being done with *raw JavaScript*. Just unbelievable ;-)
Anurag
@Andy - ROFL at the that link and the SO image - http://www.doxdesk.com/img/updates/20091116-so-large.gif I guess the "use jQuery" answer is gonna reach an all-time high in Javascript now :)
Anurag