mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 06:52:59 +08:00
Compare commits
3 Commits
95e19b2c7c
...
92711d8016
Author | SHA1 | Date | |
---|---|---|---|
|
92711d8016 | ||
|
9787af0267 | ||
|
4ea1ad0bb8 |
@ -0,0 +1,11 @@
|
|||||||
|
import factorialRecursiveTCO from '../factorialRecursiveTCO';
|
||||||
|
|
||||||
|
describe('factorialRecursiveTCO', () => {
|
||||||
|
it('should calculate factorial', () => {
|
||||||
|
expect(factorialRecursiveTCO(0)).toBe(1);
|
||||||
|
expect(factorialRecursiveTCO(1)).toBe(1);
|
||||||
|
expect(factorialRecursiveTCO(5)).toBe(120);
|
||||||
|
expect(factorialRecursiveTCO(8)).toBe(40320);
|
||||||
|
expect(factorialRecursiveTCO(10)).toBe(3628800);
|
||||||
|
});
|
||||||
|
});
|
12
src/algorithms/math/factorial/factorialRecursiveTCO.js
Normal file
12
src/algorithms/math/factorial/factorialRecursiveTCO.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
/**
|
||||||
|
* @param {number} number
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export default function factorialRecursiveTCO(number) {
|
||||||
|
function fact(number, accumulator = 1) {
|
||||||
|
if (number < 2) return accumulator;
|
||||||
|
else return fact(number - 1, accumulator * number);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fact(number);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user