{"record":{"id":"10dcf6f3a012b0a2","repo":"TheAlgorithms/JavaScript","slug":"invalid-number-range-please-provide-numbers-such","errorCode":null,"errorMessage":"Invalid number range, please provide numbers such that num1 < num2","messagePattern":"Invalid number range, please provide numbers such that num1 < num2","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"Maths/CountNumbersDivisible.js","lineNumber":32,"sourceCode":" *\n * @param {number} num1\n * @param {number} num2\n * @param {number} divider\n *\n * @returns {number} count of total number of divisibles\n */\nconst countNumbersDivisible = (num1, num2, divider) => {\n  if (\n    typeof num1 !== 'number' ||\n    typeof num2 !== 'number' ||\n    typeof divider !== 'number'\n  ) {\n    throw new Error('Invalid input, please pass only numbers')\n  }\n\n  // Valid number range is num1 < num2, otherwise throw error\n  if (num1 > num2) {\n    throw new Error(\n      'Invalid number range, please provide numbers such that num1 < num2'\n    )\n  }\n\n  // if divider is out of range then return 0\n  // as in such case no divisible exists\n  if (divider > num2) {\n    return 0\n  }\n\n  // Find the number of multiples of divider for num1 and num2\n  // integer division part\n  const num1Multiplier = num1 / divider\n  const num2Multiplier = num2 / divider\n\n  // The count of numbers divisibles by divider between num1 and num2\n  let divisibleCount = num2Multiplier - num1Multiplier\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/CountNumbersDivisible.js#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Normalize the bounds before calling: const [lo, hi] = [num1, num2].sort((a,b)=>a-b).","Explicitly order inputs at the call site: countNumbersDivisible(Math.min(a,b), Math.max(a,b), divider).","Validate user-facing range fields and inform the user when from > to.","If you control the API contract, document that num1 must be the lower bound."],"exampleFix":"// before\nconst c = countNumbersDivisible(start, end, d) // throws if start > end\n\n// after\nconst c = countNumbersDivisible(Math.min(start, end), Math.max(start, end), d)","handlingStrategy":"validation","validationCode":"const lo = Math.min(num1, num2)\nconst hi = Math.max(num1, num2)\nconst c = countNumbersDivisible(lo, hi, divider)","typeGuard":null,"tryCatchPattern":"try {\n  c = countNumbersDivisible(a, b, d)\n} catch (e) {\n  if (e instanceof Error && /num1 < num2/.test(e.message)) {\n    // swapped bounds — retry with min/max\n    c = countNumbersDivisible(Math.min(a,b), Math.max(a,b), d)\n  } else throw e\n}","preventionTips":["Normalize bounds with Math.min/Math.max before calling.","Equality is allowed (num1 === num2 does not throw), so do not over-restrict.","Validate range inputs in the UI to inform users when from > to."],"tags":["validation","range","ordering","numeric"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}