{"record":{"id":"de44b2247f8a99a5","repo":"TheAlgorithms/JavaScript","slug":"the-paramname-should-be-type-number","errorCode":null,"errorMessage":"The ${paramName} should be type Number","messagePattern":"The (.+?) should be type Number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/Area.js","lineNumber":167,"sourceCode":"\n/**\n * @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":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Area.js#L149-L185","documentation":"Thrown by the internal validateNumericParam helper (Area.js:166) as a TypeError when an argument passed to any area/surface-area function is not of type 'number'. The helper is shared by surfaceAreaCube, surfaceAreaSphere, areaRectangle, areaSquare, areaTriangle, areaParallelogram, areaTrapezium, areaCircle, areaRhombus and others. The library uses strict typeof checking, so string-encoded numbers like '5' are rejected. paramName in the message reflects the human-readable label passed by each caller (e.g. 'diagonal one').","triggerScenarios":"Call areaRhombus('12', 10) with string arguments; pass undefined when an optional parameter is omitted (e.g. areaRectangle(5) where height is undefined); pass null from a JSON source where a field was absent; pass a boolean or object accidentally.","commonSituations":"Values read from DOM inputs (always strings) or URL query params fed directly to the function; data from JSON.parse where numbers were quoted as strings in the source; a refactored function signature where a previously defaulted param is now required and callers were not updated.","solutions":["Coerce inputs explicitly with Number() or parseFloat() before calling, and check for NaN afterwards.","If reading from HTML inputs, use the valueAsNumber property instead of value to get a number directly.","Audit the call site for undefined arising from optional/destructured parameters with no default.","Add a type guard or use TypeScript so non-number arguments are caught at compile time."],"exampleFix":"// before\nconst a = areaRhombus(inputEl.value, otherInput) // value is a string\n\n// after\nconst d1 = Number(inputEl.value)\nconst d2 = Number(otherInput)\nif (Number.isNaN(d1) || Number.isNaN(d2)) throw new TypeError('non-numeric input')\nconst a = areaRhombus(d1, d2)","handlingStrategy":"type-guard","validationCode":"const asNumber = (v) => (typeof v === 'number' && !Number.isNaN(v)) ? v : NaN\nconst d1 = asNumber(diagonal1)\nconst d2 = asNumber(diagonal2)\nif (Number.isNaN(d1) || Number.isNaN(d2)) throw new TypeError('non-numeric input')\nconst a = areaRhombus(d1, d2)","typeGuard":"const isNumber = (v) => typeof v === 'number' && !Number.isNaN(v)","tryCatchPattern":"try {\n  result = areaRhombus(d1, d2)\n} catch (e) {\n  if (e instanceof TypeError && /should be type Number/.test(e.message)) {\n    // handle non-numeric input at boundary\n  } else throw e\n}","preventionTips":["Always read form inputs with valueAsNumber rather than value to get numbers directly.","Use TypeScript and type function parameters explicitly as number.","Centralize numeric coercion in a helper applied at every external boundary."],"tags":["type-check","input-validation","numeric","typeof"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}