TheAlgorithms/JavaScript · error · Error
Dimension must be positive
Error message
Dimension must be positive
What it means
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.
Source
Thrown at Project-Euler/Problem028.js:25
*
* 21 22 23 24 25
* 20 07 08 09 10
* 19 06 01 02 11
* 18 05 04 03 12
* 17 16 15 14 13
*
* It can be verified that the sum of the numbers on the diagonals is 101.
* What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
*
* @author ddaniel27
*/
function problem28(dim) {
if (dim % 2 === 0) {
throw new Error('Dimension must be odd')
}
if (dim < 1) {
throw new Error('Dimension must be positive')
}
let result = 1
for (let i = 3; i <= dim; i += 2) {
/**
* Adding more dimensions to the matrix, we will find at the top-right corner the follow sequence:
* 01, 09, 25, 49, 81, 121, 169, ...
* So this can be expressed as:
* i^2, where i is all odd numbers
*
* Also, we can know which numbers are in each corner dimension
* Just develop the sequence counter clockwise from top-right corner like this:
* First corner: i^2
* Second corner: i^2 - (i - 1) | The "i - 1" is the distance between corners in each dimension
* Third corner: i^2 - 2 * (i - 1)
* Fourth corner: i^2 - 3 * (i - 1)
*
* Doing the sum of each corner and simplifying, we found that the result for each dimension is:View on GitHub (pinned to 5c39e87a9a)
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.
Example fix
// before
problem28(computedDim) // could be -1
// after
const d = Number(computedDim)
if (!Number.isFinite(d) || d < 1 || d % 2 === 0) throw new RangeError('dim must be a positive odd integer')
problem28(d) Defensive patterns
Strategy: validation
Validate before calling
const d = Number(dim)
if (!Number.isFinite(d) || d < 1) {
throw new RangeError('dim must be a positive finite number')
}
problem28(d) Type guard
const isPositiveFinite = (x) => typeof x === 'number' && Number.isFinite(x) && x >= 1
Prevention
- 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.
When it happens
Trigger: Call `problem28(-1)`, `problem28(-3)`, `problem28(-7)`. Zero and negative even numbers hit the 'must be odd' branch first.
Common situations: Sign errors in computed dimensions, defensive defaults of -1 meaning 'unset', or arithmetic that produced a negative size.
Related errors
- The ${noName} only accepts positive values
- No natural numbers exist below 1
- Fibonacci sequence limit can't be less than 1
- Invalid Input
- Please provide number greater that 1
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/7d80d10ffc62b09f.
Report an issue: GitHub.