Skip to content

feat(factorial): create exercise #441

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
13 changes: 13 additions & 0 deletions 13_factorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Exercise 13 - Factorial

Create a function that takes an input integer greater than or equal to 1 and returns the product of all its predecessing integers. If 0 is input it returns 1. It should accept strings that when converted to a number are valid inputs. For invalid inputs it returns `undefined`.

For example:

```javascript
factorial(2); // 2 * 1, Output: 2
factorial('4'); // 4 * 3 * 2 * 1, Output: 24
factorial(5); // 5 * 4 * 3 * 2 * 1, Output: 120
factorial(7.2); // Output: undefined
factorial(0); // Output: 1
```
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;
40 changes: 40 additions & 0 deletions 13_factorial/factorial.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const factorial = require("./factorial-solution");

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('DOES accept strings', () => {
expect(factorial("0")).toBe(1);
});
test.skip('DOES accept strings', () => {
expect(factorial("1")).toBe(1);
});
test.skip('DOES accept strings', () => {
expect(factorial("2")).toBe(2);
});
test.skip('DOES accept strings', () => {
expect(factorial("8")).toBe(40320);
});
});
14 changes: 14 additions & 0 deletions 13_factorial/solution/factorial-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const factorial = function(n) {
// a negative number will always contain a '-'
// a non-integer will always contain a '.'
// the [-.] regex literal will match strings that contain a '-' or a '.'
// n + '' converts n to a string, which is required for the Regex.prototype.test method
if (/[-.]/.test(n + '')) return;

// +n converts n to a number
if (+n === 0) return 1;
return n * factorial(n - 1);
};

// Do not edit below this line
module.exports = factorial;
40 changes: 40 additions & 0 deletions 13_factorial/solution/factorial-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const factorial = require("./factorial-solution");

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('DOES accept strings', () => {
expect(factorial("0")).toBe(1);
});
test.skip('DOES accept strings', () => {
expect(factorial("1")).toBe(1);
});
test.skip('DOES accept strings', () => {
expect(factorial("2")).toBe(2);
});
test.skip('DOES accept strings', () => {
expect(factorial("8")).toBe(40320);
});
});