How to Determine If a Number Is Odd in JavaScript

How to determine if a number is odd in JavaScript

Use the below code:

function isOdd(num) { return num % 2;}console.log("1 is " + isOdd(1));console.log("2 is " + isOdd(2));console.log("3 is " + isOdd(3));console.log("4 is " + isOdd(4));

How to determine if a number is odd or even in Java script

Use the modulus operator

if(intellect % 2 == 0)
{
alert ('is even');
}
else
{
alert('is odd');
}

Testing whether a value is odd or even

Use modulus:

function isEven(n) {
return n % 2 == 0;
}

function isOdd(n) {
return Math.abs(n % 2) == 1;
}

You can check that any value in Javascript can be coerced to a number with:

Number.isFinite(parseFloat(n))

This check should preferably be done outside the isEven and isOdd functions, so you don't have to duplicate error handling in both functions.

about detecting if a number is odd or even in javascript

In JavaScript, anything that is not "falsy" is true. So, your string "Give a number" is considered to be true.

Report even or odd number in JS

You are not returning anything, just outputing it in the console.

function even_or_odd(n) {
if(n % 2 === 0) {
return 'Even';
} else {
return 'Odd';
}
};

How do I check if an integer is even or odd?

Use the modulo (%) operator to check if there's a remainder when dividing by 2:

if (x % 2) { /* x is odd */ }

A few people have criticized my answer above stating that using x & 1 is "faster" or "more efficient". I do not believe this to be the case.

Out of curiosity, I created two trivial test case programs:

/* modulo.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x % 2)
printf("%d is odd\n", x);
return 0;
}

/* and.c */
#include <stdio.h>

int main(void)
{
int x;
for (x = 0; x < 10; x++)
if (x & 1)
printf("%d is odd\n", x);
return 0;
}

I then compiled these with gcc 4.1.3 on one of my machines 5 different times:

  • With no optimization flags.
  • With -O
  • With -Os
  • With -O2
  • With -O3

I examined the assembly output of each compile (using gcc -S) and found that in each case, the output for and.c and modulo.c were identical (they both used the andl $1, %eax instruction). I doubt this is a "new" feature, and I suspect it dates back to ancient versions. I also doubt any modern (made in the past 20 years) non-arcane compiler, commercial or open source, lacks such optimization. I would test on other compilers, but I don't have any available at the moment.

If anyone else would care to test other compilers and/or platform targets, and gets a different result, I'd be very interested to know.

Finally, the modulo version is guaranteed by the standard to work whether the integer is positive, negative or zero, regardless of the implementation's representation of signed integers. The bitwise-and version is not. Yes, I realise two's complement is somewhat ubiquitous, so this is not really an issue.

Check Odd numbers without modulo operator

Hi you can do it with bitwise AND (&) operator to check if a number is even or odd.

function testodd(num) {
if((num & 1) == 0){
return true
}
return false;
}

var output = testodd(17);
console.log(output); // --> false
var output = testodd(-16);
console.log(output); // --> true
var output = testodd(0);
console.log(output); // --> true

How does '&' work in relation to odd and even? In JS

In binary notation the right-most bit is the ones place:

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
...etc

So as you can see every odd number ends in 1 and every even ends in 0.

When you use & you are making a bit-wise and calculation. When you do X & 1, are you comparing each bit of X against 1 or in binary: 00001 (you can keep extending the zeros to the left) and testing wether both bits are 1.

So for example 22 is 10110 in binary 22 & 1 looks each bit and test if both are true:

1 0 1 1 0
0 0 0 0 1 < no bits are 1 in both number
---------
0 0 0 0 0 < all zeros == 0 so 22 is even

23 is 10111:

1 0 1 1 1
0 0 0 0 1 the last bit is one in both numbers
---------
0 0 0 0 1 < 1 so 23 is odd

Since the last bit is always 1 in odd numbers x & 1 will always be one for odd numbers and zero for evens.



Related Topics



Leave a reply



Submit