Skip to content

Commit c5ed81d

Browse files
m-maksyutintrekhleb
authored andcommitted
Add recursive factorial function (trekhleb#85)
* Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph * Fix the value returned by DisjointSet union * Add recursive factorial function
1 parent 65f08db commit c5ed81d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import factorialRecursive from '../factorialRecursive';
2+
3+
describe('factorialRecursive', () => {
4+
it('should calculate factorial', () => {
5+
expect(factorialRecursive(0)).toBe(1);
6+
expect(factorialRecursive(1)).toBe(1);
7+
expect(factorialRecursive(5)).toBe(120);
8+
expect(factorialRecursive(8)).toBe(40320);
9+
expect(factorialRecursive(10)).toBe(3628800);
10+
});
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} number
3+
* @return {number}
4+
*/
5+
export default function factorialRecursive(number) {
6+
return number > 1 ? number * factorialRecursive(number - 1) : 1;
7+
}

0 commit comments

Comments
 (0)