{"record":{"id":"e77c4abaddfeafb4","repo":"TheAlgorithms/JavaScript","slug":"the-noname-only-accepts-positive-values","errorCode":null,"errorMessage":"The ${noName} only accepts positive values","messagePattern":"The (.+?) only accepts positive values","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Maths/Volume.js","lineNumber":119,"sourceCode":"  isNumber(radius, 'Radius')\n  return (4 / 3) * Math.PI * radius ** 3\n}\n\n/*\n  Calculate the volume for a Hemisphere\n  Reference: https://www.cuemath.com/measurement/volume-of-hemisphere/\n  return (2 * PI * radius^3)/3\n*/\nconst volHemisphere = (radius) => {\n  isNumber(radius, 'Radius')\n  return (2.0 * Math.PI * radius ** 3) / 3.0\n}\n\nconst isNumber = (number, noName = 'number') => {\n  if (typeof number !== 'number') {\n    throw new TypeError('The ' + noName + ' should be Number type')\n  } else if (number < 0 || !Number.isFinite(number)) {\n    throw new Error('The ' + noName + ' only accepts positive values')\n  }\n}\n\nexport {\n  volCuboid,\n  volCube,\n  volCone,\n  volPyramid,\n  volCylinder,\n  volTriangularPrism,\n  volPentagonalPrism,\n  volSphere,\n  volHemisphere\n}\n","sourceCodeStart":101,"sourceCodeEnd":134,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Volume.js#L101-L134","documentation":"The second branch of the shared `isNumber` helper: thrown when the argument IS a number but is negative (`< 0`) or non-finite (NaN, +/-Infinity). Because volumes require positive radii/lengths, the helper rejects these. Note Infinity passes the typeof check then fails here, not in the TypeError branch.","triggerScenarios":"Call `volHemisphere(-5)`, `volCylinder(NaN, 10)`, `volSphere(Infinity)`, `volCone(3, -2)`. Negative dimensions or non-finite numeric values trigger it.","commonSituations":"Sign errors in user input (negative lengths), parsing failures that produce NaN before reaching the function, or unbounded computations (`1/0`) forwarded as a radius.","solutions":["Validate `Number.isFinite(x) && x >= 0` before calling.","Reject or clamp negative inputs at the form/validation layer.","Guard against NaN explicitly - `typeof === 'number'` does not exclude it."],"exampleFix":"// before\nvolHemisphere(parsedRadius) // parsedRadius could be -5 or NaN\n\n// after\nif (!Number.isFinite(parsedRadius) || parsedRadius < 0) {\n  throw new RangeError('radius must be a non-negative finite number')\n}\nvolHemisphere(parsedRadius)","handlingStrategy":"validation","validationCode":"if (!Number.isFinite(radius) || radius < 0) {\n  throw new RangeError('radius must be a non-negative finite number')\n}\nvolHemisphere(radius)","typeGuard":"const isPositiveFinite = (x) => typeof x === 'number' && Number.isFinite(x) && x >= 0","tryCatchPattern":null,"preventionTips":["typeof === 'number' is not enough - also exclude NaN and Infinity.","Clamp or reject negative dimensions at the UI layer.","Treat NaN as 'invalid input', not as 'zero'."],"tags":["range-validation","math","geometry","positive-value"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}