From f79709f617e535b927c8548d1d39a95a1cf10d94 Mon Sep 17 00:00:00 2001 From: John Kimani Date: Thu, 24 Aug 2023 15:25:54 +0300 Subject: [PATCH] Implement _check --- src/calculator.js | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index b46080e5..60a8bd62 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,47 +1,33 @@ -exports._check = () => { +exports._check = (x,y) => { // DRY up the codebase with this function // First, move the duplicate error checking code here // Then, invoke this function inside each of the others // HINT: you can invoke this function with exports._check() -}; - -exports.add = (x, y) => { if (typeof x !== 'number') { throw new TypeError(`${x} is not a number`); } if (typeof y !== 'number') { throw new TypeError(`${y} is not a number`); } +}; + +exports.add = (x, y) => { + exports._check(x,y); return x + y; }; exports.subtract = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x,y); return x - y; }; exports.multiply = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x,y); return x * y; }; exports.divide = (x, y) => { - if (typeof x !== 'number') { - throw new TypeError(`${x} is not a number`); - } - if (typeof y !== 'number') { - throw new TypeError(`${y} is not a number`); - } + exports._check(x,y); return x / y; };