From 865cbdba53471fe05b2d2e48d0ab4c1ce6d1f021 Mon Sep 17 00:00:00 2001 From: "github@esslinger.dev" Date: Sat, 8 Oct 2022 10:09:56 +0200 Subject: [PATCH 1/5] feat(maths): add Factorial --- Maths/Factorial.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Maths/Factorial.ts diff --git a/Maths/Factorial.ts b/Maths/Factorial.ts new file mode 100644 index 00000000..883878f4 --- /dev/null +++ b/Maths/Factorial.ts @@ -0,0 +1,20 @@ +/** + * @function Factorial + * @description Calculate the factorial of a positive integer. + * @param {number} num - A positive number. + * @return {number} - The factorial. + * @see https://en.wikipedia.org/wiki/Factorial + * @example Factorial(0) = 1 + * @example Factorial(3) = 6 + */ +export const Factorial = (num: number): number => { + if (num < 0) { + throw new Error("num must be >= 0"); + } + + if (!Number.isInteger(num)){ + throw new Error("only integers are supported"); + } + + return num === 0 ? 1 : num * Factorial(num - 1); +}; From 66611ff7b45b0c97f349050f6bb2ac722d5002a7 Mon Sep 17 00:00:00 2001 From: "github@esslinger.dev" Date: Sat, 8 Oct 2022 10:10:11 +0200 Subject: [PATCH 2/5] test(maths): add Factorial tests --- Maths/test/Factorial.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Maths/test/Factorial.test.ts diff --git a/Maths/test/Factorial.test.ts b/Maths/test/Factorial.test.ts new file mode 100644 index 00000000..daca38b1 --- /dev/null +++ b/Maths/test/Factorial.test.ts @@ -0,0 +1,21 @@ +import { Factorial } from '../Factorial'; + +describe('Factorial', () => { + + test.each([-0.1, -1, -2, -42])('should throw an error for numbers < 0 like %i', (num) => { + expect(() => Factorial(num)).toThrowError("num must be >= 0"); + }) + + test.each([0.01, 0.42, 0.5, 1.337])('should throw an error for numbers that are not integers like %i', (num) => { + expect(() => Factorial(num)).toThrowError("only integers are supported"); + }) + + test.each([[1, 1], [3, 6], [5, 120], [10, 3628800] ])('of %i should be %i', (num, expected) => { + expect(Factorial(num)).toBe(expected); + }) + + test('of 1 should be 0 by definition', () => { + expect(Factorial(0)).toBe(1); + }); + +}); From cdca414c61374ad00c4d881d2f75ce3737e6f414 Mon Sep 17 00:00:00 2001 From: "github@esslinger.dev" Date: Sat, 8 Oct 2022 10:12:37 +0200 Subject: [PATCH 3/5] chore(math): use integer instead of number in jsdoc of Factorial --- Maths/Factorial.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/Factorial.ts b/Maths/Factorial.ts index 883878f4..9de57d5c 100644 --- a/Maths/Factorial.ts +++ b/Maths/Factorial.ts @@ -1,7 +1,7 @@ /** * @function Factorial * @description Calculate the factorial of a positive integer. - * @param {number} num - A positive number. + * @param {number} num - A positive integer. * @return {number} - The factorial. * @see https://en.wikipedia.org/wiki/Factorial * @example Factorial(0) = 1 From ece0a5586dc003e3dac6ce2a82f5ea7c951936b0 Mon Sep 17 00:00:00 2001 From: "github@esslinger.dev" Date: Sat, 8 Oct 2022 10:37:47 +0200 Subject: [PATCH 4/5] style: format code --- Maths/Factorial.ts | 2 +- Maths/test/Factorial.test.ts | 35 +++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Maths/Factorial.ts b/Maths/Factorial.ts index 9de57d5c..179c2716 100644 --- a/Maths/Factorial.ts +++ b/Maths/Factorial.ts @@ -12,7 +12,7 @@ export const Factorial = (num: number): number => { throw new Error("num must be >= 0"); } - if (!Number.isInteger(num)){ + if (!Number.isInteger(num)) { throw new Error("only integers are supported"); } diff --git a/Maths/test/Factorial.test.ts b/Maths/test/Factorial.test.ts index daca38b1..110f5bf8 100644 --- a/Maths/test/Factorial.test.ts +++ b/Maths/test/Factorial.test.ts @@ -1,21 +1,28 @@ -import { Factorial } from '../Factorial'; +import { Factorial } from "../Factorial"; -describe('Factorial', () => { +describe("Factorial", () => { + test.each([-0.1, -1, -2, -42])( + "should throw an error for numbers < 0 like %i", + (num) => { + expect(() => Factorial(num)).toThrowError("num must be >= 0"); + }, + ); - test.each([-0.1, -1, -2, -42])('should throw an error for numbers < 0 like %i', (num) => { - expect(() => Factorial(num)).toThrowError("num must be >= 0"); - }) + test.each([0.01, 0.42, 0.5, 1.337])( + "should throw an error for numbers that are not integers like %i", + (num) => { + expect(() => Factorial(num)).toThrowError("only integers are supported"); + }, + ); - test.each([0.01, 0.42, 0.5, 1.337])('should throw an error for numbers that are not integers like %i', (num) => { - expect(() => Factorial(num)).toThrowError("only integers are supported"); - }) + test.each([[1, 1], [3, 6], [5, 120], [10, 3628800]])( + "of %i should be %i", + (num, expected) => { + expect(Factorial(num)).toBe(expected); + }, + ); - test.each([[1, 1], [3, 6], [5, 120], [10, 3628800] ])('of %i should be %i', (num, expected) => { - expect(Factorial(num)).toBe(expected); - }) - - test('of 1 should be 0 by definition', () => { + test("of 1 should be 0 by definition", () => { expect(Factorial(0)).toBe(1); }); - }); From 7929fb78acca7a46d075b7695fcf74c5237fa4d3 Mon Sep 17 00:00:00 2001 From: "github@esslinger.dev" Date: Sat, 8 Oct 2022 10:40:30 +0200 Subject: [PATCH 5/5] refactor: simplify code --- Maths/Factorial.ts | 12 ++++-------- Maths/test/Factorial.test.ts | 15 +++++---------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Maths/Factorial.ts b/Maths/Factorial.ts index 179c2716..1783383b 100644 --- a/Maths/Factorial.ts +++ b/Maths/Factorial.ts @@ -1,19 +1,15 @@ /** * @function Factorial - * @description Calculate the factorial of a positive integer. - * @param {number} num - A positive integer. + * @description Calculate the factorial of a natural number. + * @param {number} num - A natural number. * @return {number} - The factorial. * @see https://en.wikipedia.org/wiki/Factorial * @example Factorial(0) = 1 * @example Factorial(3) = 6 */ export const Factorial = (num: number): number => { - if (num < 0) { - throw new Error("num must be >= 0"); - } - - if (!Number.isInteger(num)) { - throw new Error("only integers are supported"); + if (num < 0 || !Number.isInteger(num)) { + throw new Error("only natural numbers are supported"); } return num === 0 ? 1 : num * Factorial(num - 1); diff --git a/Maths/test/Factorial.test.ts b/Maths/test/Factorial.test.ts index 110f5bf8..5ce5ccba 100644 --- a/Maths/test/Factorial.test.ts +++ b/Maths/test/Factorial.test.ts @@ -1,17 +1,12 @@ import { Factorial } from "../Factorial"; describe("Factorial", () => { - test.each([-0.1, -1, -2, -42])( - "should throw an error for numbers < 0 like %i", + test.each([-0.1, -1, -2, -42, 0.01, 0.42, 0.5, 1.337])( + "should throw an error for non natural number %d", (num) => { - expect(() => Factorial(num)).toThrowError("num must be >= 0"); - }, - ); - - test.each([0.01, 0.42, 0.5, 1.337])( - "should throw an error for numbers that are not integers like %i", - (num) => { - expect(() => Factorial(num)).toThrowError("only integers are supported"); + expect(() => Factorial(num)).toThrowError( + "only natural numbers are supported", + ); }, );