{"record":{"id":"73c6031b5395687e","repo":"TheAlgorithms/JavaScript","slug":"input-should-be-integer-73c603","errorCode":null,"errorMessage":"Input should be integer","messagePattern":"Input should be integer","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/FibonacciNumber.js","lineNumber":10,"sourceCode":"/**\n * @function fibonacci\n * @description Fibonacci is the sum of previous two fibonacci numbers.\n * @param {Integer} N - The input integer\n * @return {Integer} fibonacci of N.\n * @see [Fibonacci_Numbers](https://en.wikipedia.org/wiki/Fibonacci_number)\n */\nconst fibonacci = (N) => {\n  if (!Number.isInteger(N)) {\n    throw new TypeError('Input should be integer')\n  }\n\n  // memoize the last two numbers\n  let firstNumber = 0\n  let secondNumber = 1\n\n  for (let i = 1; i < N; i++) {\n    const sumOfNumbers = firstNumber + secondNumber\n    // update last two numbers\n    firstNumber = secondNumber\n    secondNumber = sumOfNumbers\n  }\n\n  return N ? secondNumber : firstNumber\n}\n\nexport { fibonacci }\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/FibonacciNumber.js#L1-L28","documentation":"Thrown by fibonacci(N) as a TypeError when !Number.isInteger(N). The iterative implementation indexes by counting loop iterations, so fractional, NaN, Infinity, or non-number inputs are rejected before the loop runs.","triggerScenarios":"fibonacci(3.2); fibonacci('7'); fibonacci(Infinity); fibonacci(undefined) (undefined is not an integer).","commonSituations":"Floats from user input or division; string values not parsed; default parameters of undefined when argument omitted.","solutions":["Sanitize at the call site: const n = Number(input); if (!Number.isInteger(n)) throw.","Parse string inputs with parseInt(value, 10) and check the result.","Provide a sensible default (e.g. 0) when the argument may be undefined.","Reject negative integers explicitly if your use case does not support them."],"exampleFix":"// before\nconst f = fibonacci(input) // throws on '7' / 3.2\n\n// after\nconst n = Number(input)\nif (!Number.isInteger(n)) throw new TypeError('N must be an integer')\nconst f = fibonacci(n)","handlingStrategy":"type-guard","validationCode":"function safeFib(input) {\n  const n = Number(input)\n  if (!Number.isInteger(n)) {\n    throw new TypeError('N must be an integer')\n  }\n  return fibonacci(n)\n}","typeGuard":"const isInteger = (n) => Number.isInteger(n)","tryCatchPattern":"try {\n  return fibonacci(n)\n} catch (e) {\n  if (e instanceof TypeError && /integer/i.test(e.message)) {\n    return fibonacci(Math.trunc(Number(n)))\n  }\n  throw e\n}","preventionTips":["Coerce with Number(input) and check isInteger before calling.","Provide an explicit default when the argument may be undefined.","Parse textual input with parseInt, never parseFloat, for index-style args.","Validate at the boundary so downstream code always sees integers."],"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"}