Skip to content

Commit 7da76e5

Browse files
author
programmiri
committed
Refactor excercise Triangle to be more readable
1 parent 6cb0d03 commit 7da76e5

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

triangle/triangle.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@ class Triangle {
55
this.c = c;
66
}
77

8-
kind() {
8+
isValidTriangle() {
99
if (this.a + this.b + this.c <= 0) {
10-
throw new Error();
10+
return false;
1111
}
1212

1313
if (this.a < 0 || this.b < 0 || this.c < 0) {
14-
throw new Error();
14+
return false;
1515
}
1616

1717
if (
1818
this.a + this.b < this.c ||
1919
this.a + this.c < this.b ||
2020
this.b + this.c < this.a
2121
) {
22-
throw new Error();
22+
return false;
2323
}
2424

25-
if (this.a === this.b && this.b === this.c) {
25+
return true;
26+
}
27+
28+
isEquilateral() {
29+
return this.a === this.b && this.b === this.c;
30+
}
31+
32+
isIsosceles() {
33+
return this.a === this.b || this.a === this.c || this.b === this.c;
34+
}
35+
36+
kind() {
37+
if (!this.isValidTriangle()) {
38+
throw new Error();
39+
}
40+
if (this.isEquilateral()) {
2641
return 'equilateral';
2742
}
28-
if (this.a === this.b || this.a === this.c || this.b === this.c) {
43+
44+
if (this.isIsosceles()) {
2945
return 'isosceles';
3046
}
3147
return 'scalene';

0 commit comments

Comments
 (0)