본문으로 건너뛰기

isOdd

Determines if a number is odd with proper handling for negative numbers. Checks whether the given number is not divisible by 2. Uses appropriate modulo comparison based on the sign of the number to handle JavaScript's modulo behavior with negative numbers correctly.

Signature

const isOdd: (value: number) => boolean

Parameters

NameTypeDescription
value-Number to test for oddness

Returns

True if the number is odd, false if even

Examples

Basic odd number detection

import { isOdd } from '@winglet/common-utils';

console.log(isOdd(1)); // true
console.log(isOdd(3)); // true
console.log(isOdd(5)); // true
console.log(isOdd(2)); // false
console.log(isOdd(4)); // false
console.log(isOdd(0)); // false (zero is even)

Negative numbers and edge cases

// Negative numbers (handled correctly)
console.log(isOdd(-1)); // true
console.log(isOdd(-3)); // true
console.log(isOdd(-5)); // true
console.log(isOdd(-2)); // false
console.log(isOdd(-4)); // false

// Large numbers
console.log(isOdd(999999)); // true
console.log(isOdd(1000001)); // true

Playground

import { isOdd } from '@winglet/common-utils';

console.log(isOdd(1)); // true
console.log(isOdd(3)); // true
console.log(isOdd(5)); // true
console.log(isOdd(2)); // false
console.log(isOdd(4)); // false
console.log(isOdd(0)); // false (zero is even)

Notes

Implementation Details:

  • Handles JavaScript's modulo behavior with negative numbers
  • For positive numbers: checks if value % 2 === 1
  • For negative numbers: checks if value % 2 === -1
  • Zero is correctly identified as even (not odd)

Use Cases:

  • Array filtering (odd indices, odd values)
  • Alternating patterns complementary to even detection
  • Mathematical algorithms requiring parity checks
  • Game logic (turn-based systems, player identification)
  • Data categorization and validation
  • Optimization algorithms using odd/even properties

Performance: O(1) time and space complexity - single modulo operation with conditional check for sign handling.