{"record":{"id":"d3330c9392bb730d","repo":"TheAlgorithms/JavaScript","slug":"invalid-input-d3330c","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem044.js","lineNumber":16,"sourceCode":"/**\n * Problem 44 - Pentagon numbers\n *\n * @see {@link https://projecteuler.net/problem=44}\n *\n * Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten pentagonal numbers are:\n * 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...\n * It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 − 22 = 48, is not pentagonal.\n * Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk − Pj| is minimised; what is the value of D?\n *\n * @author ddaniel27\n */\n\nfunction problem44(k) {\n  if (k < 1) {\n    throw new Error('Invalid Input')\n  }\n\n  while (true) {\n    k++\n    const n = (k * (3 * k - 1)) / 2 // calculate Pk\n\n    for (let j = k - 1; j > 0; j--) {\n      const m = (j * (3 * j - 1)) / 2 // calculate all Pj < Pk\n      if (isPentagonal(n - m) && isPentagonal(n + m)) {\n        // Check sum and difference\n        return n - m // return D\n      }\n    }\n  }\n}\n\n/**\n * Function to check if a number is pentagonal or not","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem044.js#L1-L34","documentation":"Thrown by `problem44(k)` (Project Euler #44, pentagonal numbers) when `k < 1`. The function immediately increments `k` and loops, so a starting k below 1 has no meaning. The message is generic. Non-number inputs bypass the guard.","triggerScenarios":"Call `problem44(0)`, `problem44(-2)`. `k = 1` passes and starts the search.","commonSituations":"Defaulting a 'starting index' field to 0, off-by-one from 0-indexed vs 1-indexed caller logic, or a param parsed as 0.","solutions":["Validate `k >= 1` and is a finite integer upstream.","Coerce with `Number(k)` so string/undefined inputs are caught.","Note the function loops indefinitely until a pair is found - consider a timeout wrapper for large inputs."],"exampleFix":"// before\nproblem44(startIndex) // could be 0\n\n// after\nconst k = Number(startIndex)\nif (!Number.isInteger(k) || k < 1) throw new RangeError('k must be an integer >= 1')\nproblem44(k)","handlingStrategy":"validation","validationCode":"const k = Number(startIndex)\nif (!Number.isInteger(k) || k < 1) {\n  throw new RangeError('k must be an integer >= 1')\n}\nproblem44(k)","typeGuard":"const isPositiveInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1","tryCatchPattern":null,"preventionTips":["Coerce with Number() so undefined/NaN do not bypass the guard.","Note the function loops indefinitely until a pair is found - wrap with a timeout for safety.","Distinguish 0-indexed vs 1-indexed caller logic."],"tags":["range-validation","project-euler","pentagonal-numbers","generic-message"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}