TheAlgorithms/JavaScript · warning · Error

Invalid number range, please provide numbers such that num1

Error message

Invalid number range, please provide numbers such that num1 < num2

What it means

Thrown by countNumbersDivisible(num1, num2, divider) (CountNumbersDivisible.js:31) as a plain Error when num1 is strictly greater than num2. The function defines the valid range as num1 <= num2 and rejects inverted ranges rather than swapping them. This is a semantic check that runs after the type check passes. Note: equality (num1 === num2) is allowed and does not throw.

Source

Thrown at Maths/CountNumbersDivisible.js:32

 *
 * @param {number} num1
 * @param {number} num2
 * @param {number} divider
 *
 * @returns {number} count of total number of divisibles
 */
const countNumbersDivisible = (num1, num2, divider) => {
  if (
    typeof num1 !== 'number' ||
    typeof num2 !== 'number' ||
    typeof divider !== 'number'
  ) {
    throw new Error('Invalid input, please pass only numbers')
  }

  // Valid number range is num1 < num2, otherwise throw error
  if (num1 > num2) {
    throw new Error(
      'Invalid number range, please provide numbers such that num1 < num2'
    )
  }

  // if divider is out of range then return 0
  // as in such case no divisible exists
  if (divider > num2) {
    return 0
  }

  // Find the number of multiples of divider for num1 and num2
  // integer division part
  const num1Multiplier = num1 / divider
  const num2Multiplier = num2 / divider

  // The count of numbers divisibles by divider between num1 and num2
  let divisibleCount = num2Multiplier - num1Multiplier

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Normalize the bounds before calling: const [lo, hi] = [num1, num2].sort((a,b)=>a-b).
  2. Explicitly order inputs at the call site: countNumbersDivisible(Math.min(a,b), Math.max(a,b), divider).
  3. Validate user-facing range fields and inform the user when from > to.
  4. If you control the API contract, document that num1 must be the lower bound.

Example fix

// before
const c = countNumbersDivisible(start, end, d) // throws if start > end

// after
const c = countNumbersDivisible(Math.min(start, end), Math.max(start, end), d)
Defensive patterns

Strategy: validation

Validate before calling

const lo = Math.min(num1, num2)
const hi = Math.max(num1, num2)
const c = countNumbersDivisible(lo, hi, divider)

Try / catch

try {
  c = countNumbersDivisible(a, b, d)
} catch (e) {
  if (e instanceof Error && /num1 < num2/.test(e.message)) {
    // swapped bounds — retry with min/max
    c = countNumbersDivisible(Math.min(a,b), Math.max(a,b), d)
  } else throw e
}

Prevention

When it happens

Trigger: Call countNumbersDivisible(10, 1, 2) with bounds in descending order; user inputs where 'from' and 'to' fields were swapped; a sort or comparison that assigned the larger value to num1 by mistake.

Common situations: UI where the user can enter min/max in either order; date/number range pickers that do not enforce ordering; refactored code that previously auto-swapped but no longer does.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/10dcf6f3a012b0a2. Report an issue: GitHub.