{"record":{"id":"0de90919010b9ca0","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-please-pass-only-numbers","errorCode":null,"errorMessage":"Invalid input, please pass only numbers","messagePattern":"Invalid input, please pass only numbers","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/CountNumbersDivisible.js","lineNumber":27,"sourceCode":" * @author Chetan07j\n */\n\n/**\n * Function to find total divisibles in given range\n *\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","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/CountNumbersDivisible.js#L9-L45","documentation":"Thrown by countNumbersDivisible(num1, num2, divider) (CountNumbersDivisible.js:26) as a plain Error when any of the three arguments is not of type 'number'. The function counts integers in [num1, num2] divisible by divider using integer division, so all three must be genuine numbers. Note it throws plain Error rather than TypeError despite being a type check.","triggerScenarios":"Call countNumbersDivisible('1', '10', '2') with strings from input fields; omit an argument so it becomes undefined (e.g. countNumbersDivisible(1, 10)); pass null from a config object with missing keys.","commonSituations":"Range bounds read from URL params or form inputs (always strings); optional config parameters that default to undefined; destructured values from JSON with missing fields.","solutions":["Coerce all three arguments with Number() and check Number.isFinite on each before calling.","Ensure no argument is omitted — the function has no default parameters.","Wrap in try/catch targeting plain Error (not TypeError).","Validate at the boundary with a shared numeric-args helper."],"exampleFix":"// before\nconst c = countNumbersDivisible(min, max, step) // any may be a string\n\n// after\nconst [a, b, d] = [min, max, step].map(Number)\nif (![a, b, d].every(Number.isFinite)) throw new TypeError('non-numeric args')\nconst c = countNumbersDivisible(a, b, d)","handlingStrategy":"validation","validationCode":"const [a, b, d] = [num1, num2, divider].map(Number)\nif (![a, b, d].every(Number.isFinite)) {\n  throw new TypeError('all three arguments must be finite numbers')\n}\nconst c = countNumbersDivisible(a, b, d)","typeGuard":"const areAllNumbers = (...vals) => vals.every(v => typeof v === 'number' && Number.isFinite(v))","tryCatchPattern":"try {\n  c = countNumbersDivisible(a, b, d)\n} catch (e) {\n  if (e instanceof Error && /please pass only numbers/.test(e.message)) {\n    // non-number arg — coerce and retry\n  } else throw e\n}","preventionTips":["All three arguments are mandatory; the function has no defaults.","This is a plain Error, not TypeError.","Coerce URL/form params with Number() at the boundary."],"tags":["type-check","input-validation","numeric","range"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}