{"record":{"id":"b43112f365c3645f","repo":"TheAlgorithms/JavaScript","slug":"dimension-must-be-odd","errorCode":null,"errorMessage":"Dimension must be odd","messagePattern":"Dimension must be odd","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Project-Euler/Problem028.js","lineNumber":22,"sourceCode":" * @see {@link https://projecteuler.net/problem=28}\n *\n * Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:\n *\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)","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Project-Euler/Problem028.js#L4-L40","documentation":"Thrown by `problem28(dim)` (Project Euler #28, number spiral diagonals) when `dim % 2 === 0`. The spiral formula assumes an odd-dimensioned square (a centre cell plus concentric odd rings), so even dimensions are rejected first. The check uses `%`, which coerces strings - so `'5' % 2` is 1 and the string passes, then later arithmetic may misbehave.","triggerScenarios":"Call `problem28(2)`, `problem28(100)`, `problem28(0)`. Negative even numbers like -2 also trigger this branch; negative odd numbers pass and hit the positivity check next.","commonSituations":"UI spinners that allow any integer, grid-size config from a request param, or confusion between 'size' and 'number of rings'.","solutions":["Validate `Number.isInteger(dim) && dim % 2 === 1 && dim >= 1` upstream.","Coerce with `Number(dim)` so string inputs are handled deterministically.","If even dimensions must be supported in your domain, pick odd-floor or odd-ceil before calling."],"exampleFix":"// before\nproblem28(gridSize) // gridSize could be 100\n\n// after\nconst d = Number(gridSize)\nif (!Number.isInteger(d) || d % 2 === 0 || d < 1) throw new RangeError('dim must be a positive odd integer')\nproblem28(d)","handlingStrategy":"validation","validationCode":"const d = Number(dim)\nif (!Number.isInteger(d) || d % 2 === 0 || d < 1) {\n  throw new RangeError('dim must be a positive odd integer')\n}\nproblem28(d)","typeGuard":"const isPositiveOddInt = (x) => typeof x === 'number' && Number.isInteger(x) && x >= 1 && x % 2 === 1","tryCatchPattern":null,"preventionTips":["Coerce with Number() - the % operator coerces strings, hiding type errors.","Validate parity, positivity, and integrality in one upstream check.","Round to nearest odd if your domain must accept even grid sizes."],"tags":["range-validation","project-euler","parity","spiral"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}