From 8b96d09fa9b9810740e5c389cd6d39dc0ee2b02a Mon Sep 17 00:00:00 2001 From: xortiz Date: Mon, 1 Jan 2018 13:15:44 -0500 Subject: [PATCH] reduced repetition Modified and created some functions within calculator.js that would reduced repeated code. Also allowed written tests to run as well as verify that they passed correctly. --- src/calculator.js | 39 ++++++++++++--------------------------- src/calculator.test.js | 2 +- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index b46080e5..98af03f8 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,48 +1,33 @@ -exports._check = () => { - // 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._check = (x, y) => { + exports._throwError(x); + exports._throwError(y); }; -exports.add = (x, y) => { +exports._throwError = (x) => { 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; }; module.exports = exports; + diff --git a/src/calculator.test.js b/src/calculator.test.js index d0f85ed6..3d6b7f74 100644 --- a/src/calculator.test.js +++ b/src/calculator.test.js @@ -1,7 +1,7 @@ /* eslint-disable no-unused-expressions */ const calculator = require('./calculator'); -describe.skip('_check', () => { +describe('_check', () => { beforeEach(() => { sinon.spy(calculator, '_check'); });