From 09ab3c733db492575a5488cf439eecdc22f94e6c Mon Sep 17 00:00:00 2001 From: sosse-dev Date: Mon, 20 May 2024 06:47:38 +0800 Subject: [PATCH 1/2] Implement _check --- src/calculator.js | 38 +++++++++++++++++++++++++------------- src/calculator.test.js | 2 +- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index b46080e5..34e38893 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,47 +1,59 @@ -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) => { + if (typeof x !== "number") { + throw new TypeError(`${x} is not a number`); + } + if (typeof y !== "number") { + throw new TypeError(`${y} is not a number`); + } + + // this.add(x, y); + // this.subtract(x, y); + // this.multiply(x, y); + // this.divide(x, y); + }; exports.add = (x, y) => { - if (typeof x !== 'number') { + if (typeof x !== "number") { throw new TypeError(`${x} is not a number`); } - if (typeof y !== 'number') { + if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } + this._check(x, y) return x + y; }; exports.subtract = (x, y) => { - if (typeof x !== 'number') { + if (typeof x !== "number") { throw new TypeError(`${x} is not a number`); } - if (typeof y !== 'number') { + if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } + this._check(x, y) return x - y; }; exports.multiply = (x, y) => { - if (typeof x !== 'number') { + if (typeof x !== "number") { throw new TypeError(`${x} is not a number`); } - if (typeof y !== 'number') { + if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } + this._check(x, y) return x * y; }; exports.divide = (x, y) => { - if (typeof x !== 'number') { + if (typeof x !== "number") { throw new TypeError(`${x} is not a number`); } - if (typeof y !== 'number') { + if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } + this._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 9d428c732336944b1ff47f31c28df2f5e73deea6 Mon Sep 17 00:00:00 2001 From: sosse-dev Date: Mon, 20 May 2024 06:50:26 +0800 Subject: [PATCH 2/2] Implement _check --- src/calculator.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index 34e38893..d7bb6848 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -5,12 +5,6 @@ exports._check = (x, y) => { if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } - - // this.add(x, y); - // this.subtract(x, y); - // this.multiply(x, y); - // this.divide(x, y); - }; exports.add = (x, y) => { @@ -20,7 +14,7 @@ exports.add = (x, y) => { if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } - this._check(x, y) + this._check(x, y); return x + y; }; @@ -31,7 +25,7 @@ exports.subtract = (x, y) => { if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } - this._check(x, y) + this._check(x, y); return x - y; }; @@ -42,7 +36,7 @@ exports.multiply = (x, y) => { if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } - this._check(x, y) + this._check(x, y); return x * y; }; @@ -53,7 +47,7 @@ exports.divide = (x, y) => { if (typeof y !== "number") { throw new TypeError(`${y} is not a number`); } - this._check(x, y) + this._check(x, y); return x / y; };