{"record":{"id":"38db612b5d2e7b84","repo":"TheAlgorithms/JavaScript","slug":"input-should-be-integer","errorCode":null,"errorMessage":"Input should be integer","messagePattern":"Input should be integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/FastFibonacciNumber.js","lineNumber":20,"sourceCode":" * @function fastFibonacci\n * @description fastFibonacci is same as fibonacci algorithm by calculating the sum of previous two fibonacci numbers but in O(log(n)).\n * @param {Integer} N - The input integer\n * @return {Integer} fibonacci of N.\n * @see [Fast_Fibonacci_Numbers](https://www.geeksforgeeks.org/fast-doubling-method-to-find-the-nth-fibonacci-number/)\n */\n\n// recursive function that returns (F(n), F(n-1))\nconst fib = (N) => {\n  if (N === 0) return [0, 1]\n  const [a, b] = fib(Math.trunc(N / 2))\n  const c = a * (b * 2 - a)\n  const d = a * a + b * b\n  return N % 2 ? [d, c + d] : [c, d]\n}\n\nconst fastFibonacci = (N) => {\n  if (!Number.isInteger(N)) {\n    throw new TypeError('Input should be integer')\n  }\n  return fib(N)[0]\n}\n\nexport { fastFibonacci }\n","sourceCodeStart":2,"sourceCodeEnd":26,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/FastFibonacciNumber.js#L2-L26","documentation":"Thrown by fastFibonacci(N) as a TypeError when !Number.isInteger(N). The fast doubling algorithm relies on integer halving (Math.trunc(N/2)) and integer indexing, so non-integer, NaN, Infinity, or non-number inputs are rejected up front rather than producing garbage.","triggerScenarios":"fastFibonacci(5.5); fastFibonacci('10') (string fails isInteger); fastFibonacci(Infinity); fastFibonacci(NaN); fastFibonacci(null) (null is not an integer).","commonSituations":"Input parsed from JSON/text as a float; division result fed in without rounding; BigInt or string leaking through from upstream parsing.","solutions":["Coerce to a non-negative integer first: Math.trunc(Number(input)), then validate isInteger.","Validate at the boundary: if (!Number.isInteger(n)) reject before calling fastFibonacci.","Strip units/strings from user input and parse with parseInt(value, 10).","Reject Infinity/NaN explicitly since isInteger already excludes them but callers should fail fast."],"exampleFix":"// before\nconst f = fastFibonacci(input) // throws if input is '10' or 5.5\n\n// after\nconst n = Math.trunc(Number(input))\nif (!Number.isInteger(n) || n < 0) throw new TypeError('N must be a non-negative integer')\nconst f = fastFibonacci(n)","handlingStrategy":"type-guard","validationCode":"function safeFastFib(input) {\n  const n = Math.trunc(Number(input))\n  if (!Number.isInteger(n) || n < 0) {\n    throw new TypeError('N must be a non-negative integer')\n  }\n  return fastFibonacci(n)\n}","typeGuard":"const isNonNegInt = (n) => Number.isInteger(n) && n >= 0","tryCatchPattern":"try {\n  return fastFibonacci(n)\n} catch (e) {\n  if (e instanceof TypeError && /integer/i.test(e.message)) {\n    return fastFibonacci(Math.trunc(Number(n)))\n  }\n  throw e\n}","preventionTips":["Coerce at the boundary: Math.trunc(Number(input)) then validate.","Parse strings with parseInt(value, 10), not parseFloat.","Reject BigInt by converting with Number() first if values fit safely.","Validate isInteger at the API edge so internal callers see clean types."],"tags":["dynamic-programming","fibonacci","type-error","integer-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}