{"record":{"id":"134e721e084d0fd6","repo":"TheAlgorithms/JavaScript","slug":"index-cannot-be-a-decimal","errorCode":null,"errorMessage":"Index cannot be a Decimal","messagePattern":"Index cannot be a Decimal","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/LucasSeries.js","lineNumber":22,"sourceCode":"  Examples:\n    > loopLucas(1)\n    1\n    > loopLucas(20)\n    15127\n    > loopLucas(100)\n    792070839848372100000\n*/\n\n/**\n * @param {Number} index The position of the number you want to get from the Lucas Series\n */\nfunction lucas(index) {\n  // index can't be negative\n  if (index < 0) throw new TypeError('Index cannot be Negative')\n\n  // index can't be a decimal\n  if (Math.floor(index) !== index)\n    throw new TypeError('Index cannot be a Decimal')\n\n  let a = 2\n  let b = 1\n  for (let i = 0; i < index; i++) {\n    const temp = a + b\n    a = b\n    b = temp\n  }\n  return a\n}\n\nexport { lucas }\n","sourceCodeStart":4,"sourceCodeEnd":35,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/LucasSeries.js#L4-L35","documentation":"The lucas function requires an integer index because it uses a for-loop counter (i < index) that would not terminate correctly with a fractional value. The guard compares Math.floor(index) !== index to detect non-integers, firing after the negative check.","triggerScenarios":"Calling lucas(2.5) or lucas(3.1). Any fractional index triggers this error. Division results like 7/2 passed directly as the index are a common cause.","commonSituations":"Floating-point division results used as indices, averages or ratios passed as positions, or parsed floats not rounded.","solutions":["Pass an integer index to lucas.","Apply Math.floor() or Math.round() to computed indices before calling if truncation/rounding is intended.","Validate Number.isInteger(index) before calling."],"exampleFix":"// before\nlucas(total / 2)\n// after\nlucas(Math.floor(total / 2))","handlingStrategy":"validation","validationCode":"if (!Number.isInteger(index)) {\n  throw new TypeError('index must be an integer')\n}\nlucas(index)","typeGuard":"const isIntegerIndex = (i) => typeof i === 'number' && Number.isInteger(i)","tryCatchPattern":null,"preventionTips":["Apply Math.floor() or Math.round() to computed indices before calling.","Validate Number.isInteger before sequence lookups.","Be cautious when passing division results or averages as indices."],"tags":["math","precondition","type-validation","sequence"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}