From a8d0f4edb796606e809cf921dcc3b35d3b79c3f6 Mon Sep 17 00:00:00 2001 From: zhabinka Date: Mon, 21 Jan 2019 04:33:15 +0300 Subject: [PATCH] Triangles refactored --- javascript/triangle/triangle.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/javascript/triangle/triangle.js b/javascript/triangle/triangle.js index 0c95cd1..1beb519 100644 --- a/javascript/triangle/triangle.js +++ b/javascript/triangle/triangle.js @@ -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 {