Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: trainer2001/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: add-factorialTailRecursive
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Dec 12, 2021

  1. Add factorialRecursiveTCO

    trainer2001 committed Dec 12, 2021
    Copy the full SHA
    4ea1ad0 View commit details
  2. Copy the full SHA
    9787af0 View commit details
Showing with 23 additions and 0 deletions.
  1. +11 −0 src/algorithms/math/factorial/__test__/factorialRecursiveTCO.test.js
  2. +12 −0 src/algorithms/math/factorial/factorialRecursiveTCO.js
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions src/algorithms/math/factorial/factorialRecursiveTCO.js
Original file line number Diff line number Diff line change
@@ -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);
}