diff --git a/13_factorial/README.md b/13_factorial/README.md new file mode 100644 index 00000000000..ecd8388fa6a --- /dev/null +++ b/13_factorial/README.md @@ -0,0 +1,12 @@ +# Exercise 13 - Factorial + +Write a recursive [factorial](https://simple.wikipedia.org/wiki/Factorial) function that takes a non-negative integer, and returns the product of all positive integers less than or equal to the input integer. An input of `0` should return `1`. The function should only accept numbers, so `'4'` should not be accepted as it is a string. All invalid inputs should return `undefined`. + +For example: + +```javascript +factorial(5); // 5 * 4 * 3 * 2 * 1, Output: 120 +factorial(0); // Output: 1 +factorial(7.2); // Output: undefined +factorial('4'); // Output: undefined +``` diff --git a/13_factorial/factorial.js b/13_factorial/factorial.js new file mode 100644 index 00000000000..a88e4f6e573 --- /dev/null +++ b/13_factorial/factorial.js @@ -0,0 +1,6 @@ +const factorial = function() { + +}; + +// Do not edit below this line +module.exports = factorial; \ No newline at end of file diff --git a/13_factorial/factorial.spec.js b/13_factorial/factorial.spec.js new file mode 100644 index 00000000000..4250d5d9500 --- /dev/null +++ b/13_factorial/factorial.spec.js @@ -0,0 +1,37 @@ +const factorial = require("./factorial"); + +describe('factorial', () => { + test('4th factorial number is 24', () => { + expect(factorial(4)).toBe(24); + }); + test.skip('6th factorial number is 720', () => { + expect(factorial(6)).toBe(720); + }); + test.skip('10th factorial number is 3628800', () => { + expect(factorial(10)).toBe(3628800); + }); + test.skip('15th factorial number is 1307674368000', () => { + expect(factorial(15)).toBe(1307674368000); + }); + test.skip('25th factorial number is 1.5511210043330986e+25', () => { + expect(factorial(25)).toBe(1.5511210043330986e+25); + }); + test.skip('0th factorial number is 1', () => { + expect(factorial(0)).toBe(1); + }); + test.skip("doesn't accept negatives", () => { + expect(factorial(-25)).toBe(undefined); + }); + test.skip("doesn't accept floats", () => { + expect(factorial(5.4)).toBe(undefined); + }); + test.skip("doesn't accept a number as a string", () => { + expect(factorial('5')).toBe(undefined); + }); + test.skip("doesn't accept strings", () => { + expect(factorial('foo')).toBe(undefined); + }); + test.skip("doesn't accept arrays", () => { + expect(factorial([5])).toBe(undefined); + }); +}); diff --git a/13_factorial/solution/factorial-solution.js b/13_factorial/solution/factorial-solution.js new file mode 100644 index 00000000000..f28cc71b445 --- /dev/null +++ b/13_factorial/solution/factorial-solution.js @@ -0,0 +1,7 @@ +const factorial = function(n) { + if (!Number.isInteger(n) || n < 0) return; + if (n === 0) return 1; + return n * factorial(n - 1); +}; + +module.exports = factorial; diff --git a/13_factorial/solution/factorial-solution.spec.js b/13_factorial/solution/factorial-solution.spec.js new file mode 100644 index 00000000000..7532f54c990 --- /dev/null +++ b/13_factorial/solution/factorial-solution.spec.js @@ -0,0 +1,37 @@ +const factorial = require("./factorial-solution"); + +describe('factorial', () => { + test('4th factorial number is 24', () => { + expect(factorial(4)).toBe(24); + }); + test('6th factorial number is 720', () => { + expect(factorial(6)).toBe(720); + }); + test('10th factorial number is 3628800', () => { + expect(factorial(10)).toBe(3628800); + }); + test('15th factorial number is 1307674368000', () => { + expect(factorial(15)).toBe(1307674368000); + }); + test('25th factorial number is 1.5511210043330986e+25', () => { + expect(factorial(25)).toBe(1.5511210043330986e+25); + }); + test('0th factorial number is 1', () => { + expect(factorial(0)).toBe(1); + }); + test('doesn\'t accept negatives', () => { + expect(factorial(-25)).toBe(undefined); + }); + test('doesn\'t accept floats', () => { + expect(factorial(5.4)).toBe(undefined); + }); + test('doesn\'t accept a number as a string', () => { + expect(factorial('5')).toBe(undefined); + }); + test('doesn\'t accept strings', () => { + expect(factorial('foo')).toBe(undefined); + }); + test('doesn\'t accept arrays', () => { + expect(factorial([5])).toBe(undefined); + }); +});