From 4b720b328c478dcbf0e9a6872fe2e9324807b0c3 Mon Sep 17 00:00:00 2001 From: John-Chen92 Date: Mon, 18 Mar 2024 13:25:46 +0800 Subject: [PATCH 1/2] Implement _check --- src/calculator.js | 30 ++++++++---------------------- src/calculator.test.js | 2 +- 2 files changed, 9 insertions(+), 23 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; }; 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'); }); From 90fac3ef05658576b49f74411eef69990f8e9406 Mon Sep 17 00:00:00 2001 From: John-Chen92 Date: Mon, 18 Mar 2024 13:45:48 +0800 Subject: [PATCH 2/2] Implement _check --- src/calculator.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index 60a8bd62..b8fc9a0b 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,4 +1,4 @@ -exports._check = (x,y) => { +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 @@ -12,22 +12,22 @@ exports._check = (x,y) => { }; exports.add = (x, y) => { - exports._check(x,y); + exports._check(x, y); return x + y; }; exports.subtract = (x, y) => { - exports._check(x,y); + exports._check(x, y); return x - y; }; exports.multiply = (x, y) => { - exports._check(x,y); + exports._check(x, y); return x * y; }; exports.divide = (x, y) => { - exports._check(x,y); + exports._check(x, y); return x / y; };