HomeGorakh Raj Joshi

Factorial of Number Using Recursion

The factorial of a number is the product of all the numbers from 1 to that number.

  • #Recursion
function fact(n) {
  if (n < 0) throw new Error('Number must be positive');

  if (n === 0) return 1;

  return n * fact(n - 1);
}

console.log(fact(5));

Time Complexity O(n)/Space Complexity O(n)

Gorakh Raj Joshi

Senior Fullstack Engineer: Specializing in System Design and Architecture, Accessibility, and Frontend Interface Design

LinkedIn

GitHub

Email

All rights reserved © Gorakh Raj Joshi 2024