Compare commits

...

3 Commits

Author SHA1 Message Date
trainer2001
92711d8016
Merge 9787af0267 into 2c67b48c21 2024-04-25 08:27:05 +08:00
trainer2001
9787af0267 Add test for factorialRecursiveTCO 2021-12-12 15:40:21 +05:30
trainer2001
4ea1ad0bb8 Add factorialRecursiveTCO 2021-12-12 15:39:32 +05:30
2 changed files with 23 additions and 0 deletions

View File

@ -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);
});
});

View 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);
}