TheAlgorithms/JavaScript · error · Error
Invalid input, please pass only numbers
Error message
Invalid input, please pass only numbers
What it means
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.
Source
Thrown at Maths/CountNumbersDivisible.js:27
* @author Chetan07j
*/
/**
* Function to find total divisibles in given range
*
* @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 / dividerView on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
const c = countNumbersDivisible(min, max, step) // any may be a string
// after
const [a, b, d] = [min, max, step].map(Number)
if (![a, b, d].every(Number.isFinite)) throw new TypeError('non-numeric args')
const c = countNumbersDivisible(a, b, d) Defensive patterns
Strategy: validation
Validate before calling
const [a, b, d] = [num1, num2, divider].map(Number)
if (![a, b, d].every(Number.isFinite)) {
throw new TypeError('all three arguments must be finite numbers')
}
const c = countNumbersDivisible(a, b, d) Type guard
const areAllNumbers = (...vals) => vals.every(v => typeof v === 'number' && Number.isFinite(v))
Try / catch
try {
c = countNumbersDivisible(a, b, d)
} catch (e) {
if (e instanceof Error && /please pass only numbers/.test(e.message)) {
// non-number arg — coerce and retry
} else throw e
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The ${paramName} should be type Number
- Type of n must be number
- Argument is not a number.
- Invalid Input
- Not a Number
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/0de90919010b9ca0.
Report an issue: GitHub.