# JavaScript - Odd or even number using bit manipulation

## JavaScript Bit Manipulation: Exercise-5 with Solution

Write a JavaScript program to check a given number is odd or even using bit manipulation.

A number (i.e., integer) expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it is odd; otherwise it is even-as the last digit of any even number is 0, 2, 4, 6, or 8.

**Test Data:**

(1) -> 1 is an odd number.

(4) -> 4 is an even number.

(9) -> 9 is an odd number.

("15") -> Parameter value must be number!

**Sample Solution: **

**JavaScript Code:**

```
const check_even_odd = (n) => {
if (typeof n != "number")
{
return 'Parameter value must be number!'
}
if ((n ^ 1) == (n + 1))//even
return n +' is an even number.'
else //odd
return n +' is an odd number.'
}
console.log(check_even_odd(1))
console.log(check_even_odd(4))
console.log(check_even_odd(9))
console.log(check_even_odd("15"))
```

Sample Output:

1 is an odd number. 4 is an even number. 9 is an odd number. Parameter value must be number!

**Flowchart: **

**Live Demo: **

See the Pen javascript-bit-manipulation-exercise-5 by w3resource (@w3resource) on CodePen.

* To run the code mouse over on Result panel and click on 'RERUN' button.*

**Improve this sample solution and post your code through Disqus**

**Previous:** Next power of two of a given number.

**Next:** Check a number is a power of 4 or not.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

## JavaScript: Tips of the Day

**Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key)**

Example:

const tips_composed = (obj, fn) => Object.keys(obj) .filter(k => fn(obj[k], k)) .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); console.log(tips_composed({ p: 2, q: '4', r: 6 }, x => typeof x === 'number'));

Output:

[object Object] { p: 2, r: 6 }

**Weekly Trends**- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises