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 degree. 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.

For example − 3, 4, 5, as

3*3 + 4*4 = 5*5 = 25

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

Updated on: 16-Sep-2020

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements