Determining rightness of a triangle – JavaScript

We are required to write a JavaScript function that takes in three numbers say a, b and c representing the length of three sides of a triangle. The function should return true if those three sides represent a right-angle triangle, false otherwise.

Right Angle Triangle

A triangle is a right-angle triangle if one of the three angles in the triangle is 90 degrees. And one angle in the triangle is 90 degrees when the square of the longest side is equal to the sum of squares of the other two sides.

This is known as the Pythagorean theorem: a² + b² = c² (where c is the hypotenuse).

For example ? 3, 4, 5 forms a right triangle because:

3² + 4² = 9 + 16 = 25 = 5²

Example

Following is the code ?

const side1 = 8;
const side2 = 10;
const side3 = 6;

const isRightTriangle = (a, b, c) => {
    const con1 = (a*a) === (b*b) + (c*c);
    const con2 = (b*b) === (a*a) + (c*c);
    const con3 = (c*c) === (a*a) + (b*b);
    return con1 || con2 || con3;
};

console.log(isRightTriangle(side1, side2, side3));

Output

Following is the output in the console ?

true

How It Works

The function checks all three possible combinations since we don't know which side is the longest:

  • con1: Checks if side 'a' is the hypotenuse
  • con2: Checks if side 'b' is the hypotenuse
  • con3: Checks if side 'c' is the hypotenuse

If any condition is true, the triangle is a right triangle.

Additional Examples

// Testing more triangles
console.log(isRightTriangle(3, 4, 5));    // Classic right triangle
console.log(isRightTriangle(5, 12, 13));  // Another right triangle
console.log(isRightTriangle(1, 2, 3));    // Not a right triangle
console.log(isRightTriangle(7, 24, 25));  // Right triangle
true
true
false
true

Conclusion

To determine if a triangle is a right triangle, check if any side's square equals the sum of squares of the other two sides. This implementation efficiently tests all three possible hypotenuse configurations.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements