Triangle type ( 6 kyu )

문제

In this kata, you should calculate type of triangle with three given sides a, b and c (given in any order).

If all angles are less than 90°, this triangle is acute and function should return 1.

If one angle is strictly 90°, this triangle is right and function should return 2.

If one angle more than 90°, this triangle is obtuse and function should return 3.

If three sides cannot form triangle, or one angle is 180° (which turns triangle into segment) - function should return 0.

Input parameters are sides of given triangle. All input values are non-negative floating point or integer numbers (or both).

Examples:

triangleType(2, 4, 6); // return 0 (Not triangle)
triangleType(8, 5, 7); // return 1 (Acute, angles are approx. 82°, 38° and 60°)
triangleType(3, 4, 5); // return 2 (Right, angles are approx. 37°, 53° and exactly 90°)
triangleType(7, 12, 8); // return 3 (Obtuse, angles are approx. 34°, 106° and 40°)

삼각형이 되는지 여부와 된다면 어떤 삼각형인지 구하는 문제

풀이

function triangleType(a, b, c){
  let arr = [a, b, c].sort((a,b) => { return b-a });
  if ( arr[0] >= arr[1] + arr[2] ) return 0;
  if ( arr[0]*arr[0] < arr[1]*arr[1] + arr[2]*arr[2] ) {
    return 1
  } else if ( arr[0]*arr[0] === arr[1]*arr[1] + arr[2]*arr[2] ) {
    return 2 
  } else {
    return 3
  }  
}

삼각형이 되는지 가려낸 뒤에, 모양에 따라 if문으로 리턴

다른 사람의 풀이

똑같당


+ Recent posts