Home > Web Front-end > JS Tutorial > Coding challenge | How to check if a number is odd or even using JS

Coding challenge | How to check if a number is odd or even using JS

DDD
Release: 2024-12-31 01:16:10
Original
407 people have browsed it

Coding challenge | How to check if a number is odd or even using JS

Determining whether a number is odd or even is a fundamental programming task. In JavaScript, there are several ways to accomplish this. This tutorial will walk you through two simple methods - using the modulus operator and leveraging the bitwise AND operator.

1. Using the modulus operator

The modulus operator (%) is the most common and straightforward way to determine if a number is odd or even. This operator returns the remainder of a division operation.

Key concept:

  • A number is even if it is divisible by 2 (remainder = 0).
  • A number is odd if it is not divisible by 2 (remainder ≠ 0).

Code example:

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

function isOdd(number) {
    return number % 2 !== 0;
}

// Usage:
console.log(isEven(4)); // true
console.log(isOdd(4));  // false
console.log(isEven(7)); // false
console.log(isOdd(7));  // true
Copy after login

Output:

  • isEven(4) returns true, meaning 4 is even.
  • isOdd(7) returns true, meaning 7 is odd.

2. Using the bitwise AND operator

The bitwise AND operator (&) can also be used to determine odd or even numbers. This approach relies on binary representation:

Key concept:

  • Even numbers have a 0 as the least significant bit (e.g., 2 = 10 in binary).
  • Odd numbers have a 1 as the least significant bit (e.g., 3 = 11 in binary).

How it works:
Performing number & 1 checks the last bit of the number:

  • If number & 1 === 0, the number is even.
  • If number & 1 === 1, the number is odd.

Code example:

function isEven(number) {
    return (number & 1) === 0;
}

function isOdd(number) {
    return (number & 1) === 1;
}

// Usage:
console.log(isEven(4)); // true
console.log(isOdd(4));  // false
console.log(isEven(7)); // false
console.log(isOdd(7));  // true
Copy after login

Output:

  • isEven(4) returns true, meaning 4 is even.
  • isOdd(7) returns true, meaning 7 is odd.

Comparison of methods:

Feature Modulus Operator % Bitwise AND Operator &
Readability Very easy to understand Less intuitive
Readability Slightly slower for large numbers Slightly faster for large numbers
Use Case General-purpose applications Optimised low-level operations

Which method should you use?

  • Use the Modulus operator if you want a solution that is simple and easy to understand. It’s the most common method and is suitable for most scenarios.
  • Use the bitwise AND operator if performance is critical (e.g., in high-performance code or embedded systems).

Final thoughts

Both methods are effective for checking if a number is odd or even in JavaScript. Choose the one that best fits your specific use case. Happy coding! ?

The above is the detailed content of Coding challenge | How to check if a number is odd or even using JS. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template