2

I am creating a function that returns whether the passed in number is odd Without the modulo operator. The tricky part is that it should work for NEGATIVE numbers and ZERO.

here's my codes so far:

function testodd(num) {
  return (num/2)*2==num;
}

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

Am I making some mistakes here? Or is there a better way to do this?

Tushar
  • 85,780
  • 21
  • 159
  • 179

11 Answers11

9

you can use Bitwise operator and get same result. does this help.

<script type="text/javascript">
   function oddOrEven(x) {
       return ( x & 1 ) ? "odd" : "even";
   }    
   console.log(oddOrEven(10));
</script>

For more detail about bitwise operator

Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55
Dharmesh Rakholia
  • 1,210
  • 9
  • 22
5

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
Luuuud
  • 4,206
  • 2
  • 25
  • 34
amit21186
  • 66
  • 1
  • 1
    `return num & 1 === 0;`. –  Jun 23 '17 at 13:39
  • This function `testodd` returns `false` if the number **is** odd, which seems, well, odd. –  Jun 23 '17 at 13:49
  • @torazaburo type checking isn't needed, bitwise operator is going to make it an integer anyway – Isaac Jun 24 '17 at 04:08
  • Interestingly, this method doesn't suffer from limited range of bitwise operators. An overflow occurs from `2**31-1` to `-(2**31)`, but since the former number is odd and the later is even, the overflow doesn't matter here. – le_m Jun 25 '17 at 22:31
4

Remove the decimal part after division using Math.floor.

Math.floor(num / 2) * 2 === num;

For even numbers, there is no loss in decimal value. For odd numbers, decimal point value will be lost and comparison will falsy.

Tushar
  • 85,780
  • 21
  • 159
  • 179
  • @torazaburo Well, that number literal will be interpreted as `1.2345678901234568e+39` by JavaScript and from the exponent you can already see that it is way above the faithfully representable integer range `Number.MAX_SAFE_INTEGER`. – le_m Jun 25 '17 at 22:34
4

Try a bit-wise operation

function testodd(num) {
  return num & 1; // num AND 0x1 checks for the least significant bit, indicating true or falsey
}
Isaac
  • 11,409
  • 5
  • 33
  • 45
1

Since there's already an answer I will show you an alternative away of doing it with regex

function checkOdd(num){
  console.log(/^\d*[13579]$/.test(num));
}

checkOdd(105);

Would only work with reasonably sized integers

Sahan Serasinghe
  • 1,591
  • 20
  • 33
1

Here is a horribly inefficient method using recursion:

function checkOdd(num)
{
   num = Math.abs(num);
   if(num==0)
       return false;
   else if(num==1)
       return true;
   else
       return checkOdd(num-2);
}

Of course you should never use it.

Rish
  • 583
  • 1
  • 8
  • 18
0

Try

function testodd(num){
 if num < 0{
 var number = -num
 }
 int i = 1;
 int product = 0;
 while (product <= num)
 {
    product = divisor * i;
    i++;
 }

// return remainder
return num - (product - divisor);
}
vsarunhah
  • 125
  • 2
  • 8
  • You have a syntax error, or maybe are writing in some other language. –  Jun 23 '17 at 13:47
0

Use this function to check if a number is odd or even, without using the modulo operator %. This should work for negative numbers and zero.

function checkOdd(num) {
    // your code here
    if(num<0){                 //Check if number is negative 
        num=-num;              //Convert it into positive number
    }
    let b=Math.floor(num/2)    //Taking value for loop iteration
    for(var i=1;i<=b;i++){    
         num=num-2;             //Will check the number is odd if it subtraction end to 1 by decrementing -2 to the number
         if(num==1){
             return true;      //return true if number is odd
         }
    }  
    return false;              //return false if number is even
}
Mayank_VK
  • 19
  • 1
  • 5
0

You can use isInteger method

function isEven(n){
   return Number.isInteger(n / 2);
}
0
function odd(num) {
    if (num === 0) {
        return false;
    }
    num = Math.abs(num);
    while (num >= 2) {
        num = num - 2;
    }
    if (num === 1) {
        return true;
    } else {
        return false;
    }
}
Staxlinou
  • 1,351
  • 1
  • 5
  • 20
0
  • Even number

lets take an even number say 6; 6 divided by 2 is 3;

Math.round(3) is 3;
Math.floor(3) is 3;

3===3 eveluates to true so 6 is an even number;

  • Odd number lets take an odd number say 9; 9 divided by 2 is 4.5;

    Math.round(4.5) is 5; Math.floor(4.5) is 4;

5===4 evaluates to false so 9 is an odd number;

function evenChecked(num) {
  if (Math.round(num / 2) === Math.floor(num / 2)) {
    return `${num} is even`;
  } else {
    return `${num} is odd`;
  }
}

console.log(evenChecked(23));
console.log(evenChecked(90));
console.log(evenChecked(56));
console.log(evenChecked(49));
collins
  • 1
  • 1