-
Notifications
You must be signed in to change notification settings - Fork 25
/
Acute, Obtuse, or Right?.js
36 lines (30 loc) · 1.46 KB
/
Acute, Obtuse, or Right?.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Description:
In this Kata, you will be given an array of side lengths for a triangle.Your job is to figure out whether the triangle should be classified as Right, Acute, or Obtuse. To learn more about Right, Acute, or Obtuse triangles see here - Acute and Obtuse Triangles
Recall that a in a right triangle, one of the three angles has a value of 90°, in an obtuse angle one of the three angles has a value greater than 90°, and in an acute triangle all three of the angles have measures less than 90°.
The array given will always contain 3 positive integers, and the order of the integers may be random. The side lengths may not produce a valid triangle.
Recall that the sum of the 2 shortest sides of the triangle must be greater than the largest side (hypotenuse) in order for a triangle to be considered valid.
Your function will contain a single array as an argument, such as -
function obtRhtAct([SIDE_LEN_1,SIDE_LEN_2,SIDE_LEN_3]) {
//your code here
return -1
}
Your function should return the following values -
if (triangle is obtuse) => 0
if (triangle is right) => 1
if (triangle is acute) => 2
if (triangle is invalid) => -1
*/
function obtRhtAct(arr) {
let [a,b,c] = arr.sort((a,b)=>a-b)
if (!isTriangle(a,b,c)) return -1
if (((c*c)/2)<a*a+b*b&&a*a+b*b<c*c) return 0
if (a**2+b**2>c**2&&a**2+c**2>b**2&&c**2+b**2>a**2) return 2
return 1;
}
function isTriangle(a,b,c)
{
let max = Math.max(a, b, c);
let sum = a + b + c;
return sum - max > max;
}