{"record":{"id":"8feb593345dd605a","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-8feb59","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem007.js","lineNumber":15,"sourceCode":"import { PrimeCheck } from '../Maths/PrimeCheck.js'\n\n/**\n * Find nth Prime Number\n *\n * P.S.(Project Euler - 007):\n * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.\n * What is the 10001st prime number?\n *\n * @param {Number} n\n * @returns {Number} returns the nth prime number\n */\nexport const nthPrime = (n) => {\n  if (n < 1) {\n    throw new Error('Invalid Input')\n  }\n\n  let count = 0\n  let candidateValue = 1\n  while (count < n) {\n    candidateValue++\n    if (PrimeCheck(candidateValue)) {\n      count++\n    }\n  }\n  return candidateValue\n}\n","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem007.js#L1-L28","documentation":"Thrown by `nthPrime(n)` (Project Euler #7) when `n < 1`. The function returns the n-th prime via trial counting, so n must be a positive integer. The message is generic ('Invalid Input') so check the source line for the exact condition. Non-number inputs bypass the guard.","triggerScenarios":"Call `nthPrime(0)`, `nthPrime(-3)`. `nthPrime(1)` is valid and returns 2.","commonSituations":"Defaulting a 'which prime' field to 0, off-by-one in 0-indexed vs 1-indexed logic, or a request parameter parsed as 0 when empty.","solutions":["Validate `n >= 1` and is a finite integer upstream.","Coerce with `Number(n)` so string/undefined inputs are caught.","If 0 is semantically valid in your domain, decide on a fallback (e.g. return null) before calling."],"exampleFix":"// before\nnthPrime(req.query.n) // req.query.n could be '0' or undefined\n\n// after\nconst n = Number(req.query.n)\nif (!Number.isInteger(n) || n < 1) throw new RangeError('n must be a positive integer')\nnthPrime(n)","handlingStrategy":"validation","validationCode":"const idx = Number(n)\nif (!Number.isInteger(idx) || idx < 1) {\n  throw new RangeError('n must be a positive integer')\n}\nnthPrime(idx)","typeGuard":"const isPositiveInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1","tryCatchPattern":null,"preventionTips":["Coerce request params with Number() before forwarding.","Be careful with 0-indexed vs 1-indexed caller logic.","Pick a sensible default (e.g. 1) when the field is missing."],"tags":["range-validation","project-euler","primes","generic-message"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}