アルゴリズム74-Is this a三角形?


Q.


Implement a function that accepts 3 integer values a, b, c. The function should return true if a triangle can be built with the sides of given length and false in any other case.
(In this case, all triangles must have surface greater than 0 to be accepted).

A)

function isTriangle(a,b,c)
{
  if (a <= 0 || b <= 0 || c <= 0)
    return false;
  
  let arr = [a, b, c].sort((a, b) => a - b);
  return arr[2] < arr[0] + arr[1];
}

other


新しい配列を作るのではなく、このように構造を分解することもできます!
function isTriangle(a,b,c)
{
  [a, b, c] = [a, b, c].sort((x, y) => x-y);
  
  return a+b > c;
}