{"record":{"id":"7d80d10ffc62b09f","repo":"TheAlgorithms/JavaScript","slug":"dimension-must-be-positive","errorCode":null,"errorMessage":"Dimension must be positive","messagePattern":"Dimension must be positive","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem028.js","lineNumber":25,"sourceCode":" *\n * 21 22 23 24 25\n * 20 07 08 09 10\n * 19 06 01 02 11\n * 18 05 04 03 12\n * 17 16 15 14 13\n *\n * It can be verified that the sum of the numbers on the diagonals is 101.\n * What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?\n *\n * @author ddaniel27\n */\n\nfunction problem28(dim) {\n  if (dim % 2 === 0) {\n    throw new Error('Dimension must be odd')\n  }\n  if (dim < 1) {\n    throw new Error('Dimension must be positive')\n  }\n\n  let result = 1\n  for (let i = 3; i <= dim; i += 2) {\n    /**\n     * Adding more dimensions to the matrix, we will find at the top-right corner the follow sequence:\n     * 01, 09, 25, 49, 81, 121, 169, ...\n     * So this can be expressed as:\n     * i^2, where i is all odd numbers\n     *\n     * Also, we can know which numbers are in each corner dimension\n     * Just develop the sequence counter clockwise from top-right corner like this:\n     * First corner: i^2\n     * Second corner: i^2 - (i - 1) | The \"i - 1\" is the distance between corners in each dimension\n     * Third corner: i^2 - 2 * (i - 1)\n     * Fourth corner: i^2 - 3 * (i - 1)\n     *\n     * Doing the sum of each corner and simplifying, we found that the result for each dimension is:","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem028.js#L7-L43","documentation":"Thrown by `problem28(dim)` when `dim < 1`, AFTER the oddness check has passed. So this fires for negative odd numbers (-1, -3, ...) and for `dim = 0` is impossible (0 is even, caught earlier). Note that NaN bypasses both checks because `NaN < 1` is false.","triggerScenarios":"Call `problem28(-1)`, `problem28(-3)`, `problem28(-7)`. Zero and negative even numbers hit the 'must be odd' branch first.","commonSituations":"Sign errors in computed dimensions, defensive defaults of -1 meaning 'unset', or arithmetic that produced a negative size.","solutions":["Validate `dim >= 1` (and odd) in one upstream check.","Reject negative inputs at the form/config layer.","Guard NaN explicitly - the current check lets it through."],"exampleFix":"// before\nproblem28(computedDim) // could be -1\n\n// after\nconst d = Number(computedDim)\nif (!Number.isFinite(d) || d < 1 || d % 2 === 0) throw new RangeError('dim must be a positive odd integer')\nproblem28(d)","handlingStrategy":"validation","validationCode":"const d = Number(dim)\nif (!Number.isFinite(d) || d < 1) {\n  throw new RangeError('dim must be a positive finite number')\n}\nproblem28(d)","typeGuard":"const isPositiveFinite = (x) => typeof x === 'number' && Number.isFinite(x) && x >= 1","tryCatchPattern":null,"preventionTips":["Guard NaN explicitly - the library's `< 1` check lets NaN through.","Combine positivity, oddness, and integrality in one upstream validator.","Reject negative inputs at the config layer."],"tags":["range-validation","project-euler","positive-value","nan-pitfall"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}