Skip to content

Commit

Permalink
Triangles refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabinka committed Jan 21, 2019
1 parent 61a3abf commit a8d0f4e
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions javascript/triangle/triangle.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
const typesList = [
{
type: 'illegal',
type: () => { throw new Error(); },
check: (a, b, c) => (a <= 0 || a + b < c),
},
{
type: 'equilateral',
type: () => 'equilateral',
check: (a, b, c) => (a === b && b === c),
},
{
type: 'isosceles',
type: () => 'isosceles',
check: (a, b, c) => (a === b || b === c),
},
{
type: 'scalene',
type: () => 'scalene',
check: (a, b, c) => (a !== b && b !== c),
},
];

const checker = (sides) => {
const [a, b, c] = sides.sort((x, y) => x - y);
const { type } = typesList.find(el => el.check(a, b, c));

if (type === 'illegal') {
throw new Error();
}

return type;
return typesList
.find(el => el.check(a, b, c))
.type();
};

class Triangle {
Expand Down

0 comments on commit a8d0f4e

Please sign in to comment.