{"record":{"id":"e3688c88b7e423fc","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-e3688c","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/ExponentialFunction.js","lineNumber":13,"sourceCode":"/**\n * @function exponentialFunction\n * @description Calculates the n+1 th order Taylor series approximation of exponential function e^x given n\n * @param {Integer} power\n * @param {Integer} order - 1\n * @returns exponentialFunction(2,20) = 7.3890560989301735\n * @url https://en.wikipedia.org/wiki/Exponential_function\n */\nfunction exponentialFunction(power, n) {\n  let output = 0\n  let fac = 1\n  if (isNaN(power) || isNaN(n) || n < 0) {\n    throw new TypeError('Invalid Input')\n  }\n  if (n === 0) {\n    return 1\n  }\n  for (let i = 0; i < n; i++) {\n    output += power ** i / fac\n    fac *= i + 1\n  }\n  return output\n}\n\nexport { exponentialFunction }\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/ExponentialFunction.js#L1-L26","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst r = exponentialFunction(p, order) // p may be NaN\n\n// after\nif (!Number.isFinite(p) || !Number.isFinite(order) || order < 0) {\n  throw new TypeError('power must be finite and order >= 0')\n}\nconst r = exponentialFunction(p, Math.max(0, Math.floor(order)))","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(power) || !Number.isFinite(n) || n < 0) {\n  throw new TypeError('power must be finite and order (n) must be >= 0')\n}\nconst r = exponentialFunction(power, Math.max(0, Math.floor(n)))","typeGuard":"const isValidExpArgs = (p, n) => Number.isFinite(p) && Number.isFinite(n) && n >= 0","tryCatchPattern":"try {\n  r = exponentialFunction(power, n)\n} catch (e) {\n  if (e instanceof TypeError && e.message === 'Invalid Input') {\n    // NaN or negative order — validate inputs and retry\n  } else throw e\n}","preventionTips":["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."],"tags":["type-check","input-validation","numeric","taylor-series","nan"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}