Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(factorial): create exercise #441

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 13_factorial/README.md
Original file line number Diff line number Diff line change
@@ -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
```
6 changes: 6 additions & 0 deletions 13_factorial/factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const factorial = function() {

};

// Do not edit below this line
module.exports = factorial;
37 changes: 37 additions & 0 deletions 13_factorial/factorial.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
7 changes: 7 additions & 0 deletions 13_factorial/solution/factorial-solution.js
Original file line number Diff line number Diff line change
@@ -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;
37 changes: 37 additions & 0 deletions 13_factorial/solution/factorial-solution.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});