Valid triangle edges - JavaScript


Suppose, we have three lines of length, respectively l, m and n. These three lines can only form a triangle, if the sum of any arbitrary two sides is greater than the third side.

For example, if the length of three lines is 4, 9 and 3, they cannot form a triangle because 4+3 is less than 9.

We are required to write a JavaScript function that the three numbers represents the length of three sides and checks whether they can form a triangle or not.

Example

Following is the code −

const a = 9, b = 5, c = 3;
const isValidTriangle = (a = 1, b = 1, c = 1) => {
   if(!a || !b || !c){
      return false;
   };
   const con1 = a + b > c;
   const con2 = b + c > a;
   const con3 = c + a > b;
   return con1 && con2 && con3;
};
console.log(isValidTriangle(a, b, c));

Output

Following is the output in the console −

false

Updated on: 16-Sep-2020

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements