{"record":{"id":"03ea7fe10bdf2ecc","repo":"TheAlgorithms/JavaScript","slug":"the-paramname-only-accepts-non-negative-values","errorCode":null,"errorMessage":"The ${paramName} only accepts non-negative values","messagePattern":"The (.+?) only accepts non-negative values","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/Area.js","lineNumber":169,"sourceCode":" * @function areaRhombus\n * @description Calculate the area of a rhombus.\n * @param {Integer} diagonal1 - Integer\n * @param {Integer} diagonal2 - Integer\n * @return {Integer} - (1 / 2) * diagonal1 * diagonal2\n * @see [areaRhombus](https://en.wikipedia.org/wiki/Rhombus)\n * @example areaRhombus(12, 10) = 60\n */\nconst areaRhombus = (diagonal1, diagonal2) => {\n  validateNumericParam(diagonal1, 'diagonal one')\n  validateNumericParam(diagonal2, 'diagonal two')\n  return (1 / 2) * diagonal1 * diagonal2\n}\n\nconst validateNumericParam = (param, paramName = 'param') => {\n  if (typeof param !== 'number') {\n    throw new TypeError('The ' + paramName + ' should be type Number')\n  } else if (param < 0) {\n    throw new Error('The ' + paramName + ' only accepts non-negative values')\n  }\n}\n\nexport {\n  surfaceAreaCube,\n  surfaceAreaSphere,\n  areaRectangle,\n  areaSquare,\n  areaTriangle,\n  areaParallelogram,\n  areaTrapezium,\n  areaCircle,\n  areaRhombus,\n  areaTriangleWithAllThreeSides\n}\n","sourceCodeStart":151,"sourceCodeEnd":185,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Area.js#L151-L185","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Apply Math.abs() to the value if the magnitude is what matters and sign is irrelevant.","Add an explicit precondition check: if (val < 0) throw or clamp before calling.","Trace the upstream computation that produced the negative and fix the sign at the source rather than masking it here.","Catch plain Error (not just TypeError) at the boundary if you need graceful degradation."],"exampleFix":"// before\nconst a = areaRhombus(d1, d2) // throws if d1 or d2 < 0\n\n// after\nconst a = areaRhombus(Math.abs(d1), Math.abs(d2))","handlingStrategy":"validation","validationCode":"const safeLength = (v) => {\n  if (typeof v !== 'number' || Number.isNaN(v)) return null\n  return Math.abs(v)\n}\nconst d1 = safeLength(diagonal1)\nif (d1 === null) throw new Error('invalid length')","typeGuard":"const isNonNegativeNumber = (v) => typeof v === 'number' && !Number.isNaN(v) && v >= 0","tryCatchPattern":"try {\n  result = areaRhombus(d1, d2)\n} catch (e) {\n  if (e instanceof Error && /non-negative/.test(e.message)) {\n    // negative input — fix sign upstream or use abs()\n  } else throw e\n}","preventionTips":["Apply Math.abs to any value derived from a difference before passing as a geometric length.","Note this is a plain Error, not TypeError — your catch must not be TypeError-only.","Trace negative values to their source rather than masking them with abs blindly."],"tags":["validation","numeric","negative-value","geometry"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}