코드로 우주평화

Is this a triangle? 본문

나는 이렇게 학습한다/Algorithm & SQL

Is this a triangle?

daco2020 2022. 3. 4. 12:43
반응형

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

 

 

Solution:

1. Sort a, b, c in ascending order.
2. Compare the last number (largest number) with the sum of the remaining two numbers.
3. If it is equal to or greater than, false is returned, otherwise true is returned.

 

 

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

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Abbreviate a Two Word Name  (0) 2022.03.06
Equal Sides Of An Array  (0) 2022.03.05
Sum of Numbers  (0) 2022.03.03
Grasshopper - Summation  (0) 2022.03.03
Counting Duplicates  (0) 2022.03.01