TheAlgorithms/JavaScript · error · Error

The ${paramName} only accepts non-negative values

Error message

The ${paramName} only accepts non-negative values

What it means

Thrown by the shared validateNumericParam helper (Area.js:168) as a plain Error (not TypeError) when a numeric argument is negative. Geometric area/surface functions are only defined for non-negative lengths, so the library rejects negative magnitudes after the type check passes. The message includes the paramName label supplied by the calling function (e.g. 'diagonal one'). Note this is thrown as Error, so catching TypeError specifically will miss it.

Source

Thrown at Maths/Area.js:169

 * @function areaRhombus
 * @description Calculate the area of a rhombus.
 * @param {Integer} diagonal1 - Integer
 * @param {Integer} diagonal2 - Integer
 * @return {Integer} - (1 / 2) * diagonal1 * diagonal2
 * @see [areaRhombus](https://en.wikipedia.org/wiki/Rhombus)
 * @example areaRhombus(12, 10) = 60
 */
const areaRhombus = (diagonal1, diagonal2) => {
  validateNumericParam(diagonal1, 'diagonal one')
  validateNumericParam(diagonal2, 'diagonal two')
  return (1 / 2) * diagonal1 * diagonal2
}

const validateNumericParam = (param, paramName = 'param') => {
  if (typeof param !== 'number') {
    throw new TypeError('The ' + paramName + ' should be type Number')
  } else if (param < 0) {
    throw new Error('The ' + paramName + ' only accepts non-negative values')
  }
}

export {
  surfaceAreaCube,
  surfaceAreaSphere,
  areaRectangle,
  areaSquare,
  areaTriangle,
  areaParallelogram,
  areaTrapezium,
  areaCircle,
  areaRhombus,
  areaTriangleWithAllThreeSides
}

View on GitHub (pinned to 5c39e87a9a)

Solutions

  1. Apply Math.abs() to the value if the magnitude is what matters and sign is irrelevant.
  2. Add an explicit precondition check: if (val < 0) throw or clamp before calling.
  3. Trace the upstream computation that produced the negative and fix the sign at the source rather than masking it here.
  4. Catch plain Error (not just TypeError) at the boundary if you need graceful degradation.

Example fix

// before
const a = areaRhombus(d1, d2) // throws if d1 or d2 < 0

// after
const a = areaRhombus(Math.abs(d1), Math.abs(d2))
Defensive patterns

Strategy: validation

Validate before calling

const safeLength = (v) => {
  if (typeof v !== 'number' || Number.isNaN(v)) return null
  return Math.abs(v)
}
const d1 = safeLength(diagonal1)
if (d1 === null) throw new Error('invalid length')

Type guard

const isNonNegativeNumber = (v) => typeof v === 'number' && !Number.isNaN(v) && v >= 0

Try / catch

try {
  result = areaRhombus(d1, d2)
} catch (e) {
  if (e instanceof Error && /non-negative/.test(e.message)) {
    // negative input — fix sign upstream or use abs()
  } else throw e
}

Prevention

When it happens

Trigger: Call areaRhombus(-12, 10) or any area function with a negative side/diagonal/radius; a subtraction or signed computation upstream produced a negative value passed unchecked.

Common situations: Coordinate-difference calculations (e.g. x2 - x1) that produce negatives when points are ordered unexpectedly; sensor or measurement data with sign errors; abs() forgotten on a delta computation before passing as a length.

Related errors


AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13). Data as JSON: /api/errors/03ea7fe10bdf2ecc. Report an issue: GitHub.