TheAlgorithms/JavaScript · error · TypeError
Invalid Input
Error message
Invalid Input
What it means
Thrown by exponentialFunction(power, n) (ExponentialFunction.js:13) as a TypeError when power or n is NaN, or when n (the Taylor series order) is negative. The function approximates e^power using an n+1 term Taylor expansion; a negative order would make the for-loop body never execute meaningfully and NaN inputs are meaningless. Note the parameter is named n in the signature but documented as 'order' — order must be >= 0.
Source
Thrown at Maths/ExponentialFunction.js:13
/**
* @function exponentialFunction
* @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n
* @param {Integer} power
* @param {Integer} order - 1
* @returns exponentialFunction(2,20) = 7.3890560989301735
* @url https://en.wikipedia.org/wiki/Exponential_function
*/
function exponentialFunction(power, n) {
let output = 0
let fac = 1
if (isNaN(power) || isNaN(n) || n < 0) {
throw new TypeError('Invalid Input')
}
if (n === 0) {
return 1
}
for (let i = 0; i < n; i++) {
output += power ** i / fac
fac *= i + 1
}
return output
}
export { exponentialFunction }
View on GitHub (pinned to 5c39e87a9a)
Solutions
- Validate both arguments with Number.isFinite before calling.
- Ensure the order n is a non-negative integer; clamp or reject negatives.
- Trace upstream parseFloat / parseInt results and handle NaN explicitly.
- Provide sensible defaults for optional arguments rather than leaving them undefined.
Example fix
// before
const r = exponentialFunction(p, order) // p may be NaN
// after
if (!Number.isFinite(p) || !Number.isFinite(order) || order < 0) {
throw new TypeError('power must be finite and order >= 0')
}
const r = exponentialFunction(p, Math.max(0, Math.floor(order))) Defensive patterns
Strategy: validation
Validate before calling
if (!Number.isFinite(power) || !Number.isFinite(n) || n < 0) {
throw new TypeError('power must be finite and order (n) must be >= 0')
}
const r = exponentialFunction(power, Math.max(0, Math.floor(n))) Type guard
const isValidExpArgs = (p, n) => Number.isFinite(p) && Number.isFinite(n) && n >= 0
Try / catch
try {
r = exponentialFunction(power, n)
} catch (e) {
if (e instanceof TypeError && e.message === 'Invalid Input') {
// NaN or negative order — validate inputs and retry
} else throw e
} Prevention
- Guard parseFloat results with Number.isFinite before passing as power.
- Ensure the order n is non-negative; clamp with Math.max(0, n).
- Note the second parameter is named n in code but 'order' in docs — same thing.
When it happens
Trigger: Call exponentialFunction(NaN, 5) with a NaN power; exponentialFunction(2, -1) with a negative order; pass undefined for either argument (undefined propagates to NaN via isNaN); pass a string that fails to coerce.
Common situations: Reading power from a failed parseFloat (returns NaN); defaulting n to -1 as a sentinel; arithmetic that produced NaN (0/0); optional parameter omitted without a default.
Related errors
- The ${paramName} should be type Number
- Type of n must be number
- Argument is not a number.
- Invalid input, please pass only numbers
- Not a Number
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/e3688c88b7e423fc.
Report an issue: GitHub.