From 83b4dd82909badb19ad0a10e658ace64d69cd4da Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sat, 2 Mar 2024 08:55:14 +0100 Subject: [PATCH] fix: cleanup `CheckKishnamurthyNumber` (#1626) --- Maths/CheckKishnamurthyNumber.js | 5 ++++- Maths/test/CheckKishnamurthyNumber.test.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 Maths/test/CheckKishnamurthyNumber.test.js diff --git a/Maths/CheckKishnamurthyNumber.js b/Maths/CheckKishnamurthyNumber.js index 7a0cc9a0c..5098b0fa7 100644 --- a/Maths/CheckKishnamurthyNumber.js +++ b/Maths/CheckKishnamurthyNumber.js @@ -24,7 +24,10 @@ const factorial = (n) => { const CheckKishnamurthyNumber = (number) => { // firstly, check that input is a number or not. if (typeof number !== 'number') { - return new TypeError('Argument is not a number.') + throw new TypeError('Argument is not a number.') + } + if (number === 0) { + return false } // create a variable to store the sum of all digits factorial. let sumOfAllDigitFactorial = 0 diff --git a/Maths/test/CheckKishnamurthyNumber.test.js b/Maths/test/CheckKishnamurthyNumber.test.js new file mode 100644 index 000000000..4f2e0db95 --- /dev/null +++ b/Maths/test/CheckKishnamurthyNumber.test.js @@ -0,0 +1,18 @@ +import { CheckKishnamurthyNumber } from '../CheckKishnamurthyNumber' + +describe('CheckKishnamurthyNumber', () => { + it.each([1, 2, 145, 40585])('returns true for %i', (num) => { + expect(CheckKishnamurthyNumber(num)).toBe(true) + }) + + it.each([0, 3, 4, 5, 100, 146, 1019823, -1])( + 'returns false for %i', + (num) => { + expect(CheckKishnamurthyNumber(num)).toBe(false) + } + ) + + it('should throw when input is not a number', () => { + expect(() => CheckKishnamurthyNumber('2')).toThrowError() + }) +})