TypeScript recursive factorial function
This is a factorial(factor)
function in TypeScript that uses recursion.
function factorial(factor: number): number {
if (factor <= 1) {
return 1;
}
return factor * factorial(factor - 1);
}
Jest tests:
describe(factorial.name, () => {
test("recursive factorial", () => {
expect(factorial(0)).toBe(1);
expect(factorial(1)).toBe(1);
expect(factorial(2)).toBe(2);
expect(factorial(3)).toBe(6);
expect(factorial(4)).toBe(24);
expect(factorial(5)).toBe(120);
expect(factorial(6)).toBe(720);
expect(factorial(7)).toBe(5040);
})
});