简体   繁体   中英

Check Odd numbers without modulo operator

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?

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

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

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.

Try a bit-wise operation

function testodd(num) {
  return num & 1; // num AND 0x1 checks for the least significant bit, indicating true or falsey
}

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

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.

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

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
}

You can use isInteger method

function isEven(n){
   return Number.isInteger(n / 2);
}
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;

}

}
  • 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM