From 61e45e4c9f67a8d33fdbeedbed221528e74885d0 Mon Sep 17 00:00:00 2001 From: Vijay Takbhate Date: Tue, 26 Nov 2024 12:12:48 +0530 Subject: [PATCH 1/3] Drying codebase with _check function --- src/calculator.js | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index b46080e5..c44ddeeb 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,47 +1,29 @@ 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.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; }; From ea8110ea7e7de33eb692dbb4ac8f7b7a6084fddc Mon Sep 17 00:00:00 2001 From: Vijay Takbhate Date: Tue, 26 Nov 2024 12:35:49 +0530 Subject: [PATCH 2/3] drying_codebase --- src/calculator.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/calculator.js b/src/calculator.js index c44ddeeb..dde57e95 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,4 +1,5 @@ exports._check = () => { + if (typeof x !== 'number') { throw new TypeError(`${x} is not a number`); } From cb8f69c44f5acf4531a0d0f211fd8bb587a97eba Mon Sep 17 00:00:00 2001 From: Vijay Takbhate Date: Tue, 26 Nov 2024 12:42:46 +0530 Subject: [PATCH 3/3] error_solved --- src/calculator.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/calculator.js b/src/calculator.js index dde57e95..f6505158 100644 --- a/src/calculator.js +++ b/src/calculator.js @@ -1,5 +1,4 @@ -exports._check = () => { - +exports._check = (x, y) => { if (typeof x !== 'number') { throw new TypeError(`${x} is not a number`); } @@ -9,23 +8,26 @@ exports._check = () => { }; exports.add = (x, y) => { - exports._check(x, y) - return x + y; + exports._check(x, y); + return x + y; }; exports.subtract = (x, y) => { - exports._check(x, y) - return x - y; + exports._check(x, y); + return x - y; }; exports.multiply = (x, y) => { - exports._check(x, y) - return x * y; + exports._check(x, y); + return x * y; }; exports.divide = (x, y) => { - exports._check(x, y) - return x / y; + exports._check(x, y); + if (y === 0) { + throw new Error("Cannot divide by zero"); + } + return x / y; }; module.exports = exports;