본문 바로가기
알고리즘/Codewars

[Codewars] Is this a triangle? (7 kyu) / JavaScript

by fluss 2022. 12. 21.

https://www.codewars.com/kata/56606694ec01347ce800001b

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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

 

설명:

3개의 정수 값 a, b, c를 받는 함수를 구현하세요. 주어진 변의 길이로 삼각형을 만들 수 있다면 true를 그렇지 않다면 false를 반환해야 합니다.

 

이 경우, 모든 삼각형의 면적은 0보다 커야 합니다.

 

풀이

a, b, c를 내림차순으로 정렬해서 처음 값이 나머지 값을 합한 값보다 작으면 true를 그렇지 않다면 false를 반환하게 했다.

 

코드

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

댓글