{"record":{"id":"8c546d2abbbf1aa6","repo":"TheAlgorithms/JavaScript","slug":"invalid-triangle-sides","errorCode":null,"errorMessage":"Invalid Triangle sides.","messagePattern":"Invalid Triangle sides\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Maths/Area.js","lineNumber":93,"sourceCode":" * @function areaTriangleWithAllThreeSides\n * @description Calculate the area of a triangle with the all three sides given.\n * @param {Integer} side1 - Integer\n * @param {Integer} side2 - Integer\n * @param {Integer} side3 - Integer\n * @return {Integer} - area of triangle.\n * @see [areaTriangleWithAllThreeSides](https://en.wikipedia.org/wiki/Heron%27s_formula)\n * @example areaTriangleWithAllThreeSides(5, 6, 7) = 14.7\n */\nconst areaTriangleWithAllThreeSides = (side1, side2, side3) => {\n  validateNumericParam(side1, 'side1')\n  validateNumericParam(side2, 'side2')\n  validateNumericParam(side3, 'side3')\n  if (\n    side1 + side2 <= side3 ||\n    side1 + side3 <= side2 ||\n    side2 + side3 <= side1\n  ) {\n    throw new TypeError('Invalid Triangle sides.')\n  }\n  // Finding Semi perimeter of the triangle using formula\n  const semi = (side1 + side2 + side3) / 2\n\n  // Calculating the area of the triangle\n  const area = Math.sqrt(\n    semi * (semi - side1) * (semi - side2) * (semi - side3)\n  )\n  return Number(area.toFixed(2))\n}\n\n/**\n * @function areaParallelogram\n * @description Calculate the area of a parallelogram.\n * @param {Integer} base - Integer\n * @param {Integer} height - Integer\n * @return {Integer} - base * height\n * @see [areaParallelogram](https://en.wikipedia.org/wiki/Area#Dissection,_parallelograms,_and_triangles)","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Maths/Area.js#L75-L111","documentation":"Thrown by areaTriangleWithAllThreeSides(side1, side2, side3) as a TypeError when the three given lengths violate the triangle inequality. The library enforces this before applying Heron's formula because Heron's formula would otherwise produce a NaN (square root of a negative product) for impossible triangles. The check is strict: a sum equal to the third side (degenerate triangle) is also rejected. This guard prevents silently returning mathematically meaningless results.","triggerScenarios":"Call areaTriangleWithAllThreeSides(1, 2, 10) where 1+2 <= 10; or any call where the largest side is greater than or equal to the sum of the other two (e.g. areaTriangleWithAllThreeSides(5, 5, 10) triggers because 5+5 <= 10). Passing zero-length sides where the inequality collapses (e.g. 0, 0, 5) also triggers it.","commonSituations":"User input collected from form fields arriving as strings parsed with parseInt but unvalidated for geometric feasibility; CSV/imported data with measurement errors or missing values represented as 0; unit-conversion mistakes (mm vs cm) making one side disproportionately large.","solutions":["Validate triangle inequality in your own code before calling: assert side1 + side2 > maxSide for each permutation of the three sides.","Inspect the raw inputs for zero, negative, or NaN values that may have slipped through validateNumericParam upstream.","If degenerate triangles (collinear points, zero area) are acceptable in your domain, wrap the call in try/catch and treat the failure as area 0.","Check that the three values correspond to the correct sides and were not transposed or duplicated during data binding."],"exampleFix":"// before\nconst area = areaTriangleWithAllThreeSides(a, b, c) // throws if invalid\n\n// after\nfunction safeTriangleArea(s1, s2, s3) {\n  const sides = [s1, s2, s3].sort((x, y) => x - y)\n  if (sides[0] + sides[1] <= sides[2]) return 0 // degenerate or invalid\n  return areaTriangleWithAllThreeSides(s1, s2, s3)\n}","handlingStrategy":"validation","validationCode":"function canFormTriangle(s1, s2, s3) {\n  const sides = [s1, s2, s3]\n  // all must be positive numbers\n  if (!sides.every(v => typeof v === 'number' && v > 0)) return false\n  sides.sort((a, b) => a - b)\n  return sides[0] + sides[1] > sides[2] // strict: degenerate rejected\n}\n\nif (!canFormTriangle(a, b, c)) {\n  // skip or return 0; do not call areaTriangleWithAllThreeSides\n}","typeGuard":"function isValidTriangle(s1, s2, s3) {\n  return [s1, s2, s3].every(v => typeof v === 'number' && Number.isFinite(v) && v > 0)\n    && s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1\n}","tryCatchPattern":"try {\n  const area = areaTriangleWithAllThreeSides(a, b, c)\n} catch (e) {\n  if (e instanceof TypeError && /Triangle sides/.test(e.message)) {\n    // invalid triangle — treat as zero area or surface to caller\n  } else throw e\n}","preventionTips":["Always sort the three sides and check the two smallest sum strictly greater than the largest before calling.","Reject zero-length sides in your own validator; the library also rejects them via the inequality.","Unit-test the boundary case (degenerate triangle) explicitly at your call site."],"tags":["geometry","validation","triangle-inequality","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}