← All Exercises

Factorial Calculator

basicjavascript~10min

Background

Factorials are fundamental in combinatorics, probability theory, and mathematical series expansions.

Problem

Write a function that computes the factorial of a given non-negative integer n, where n! = n * (n-1) * ... * 1.

Input

A non-negative integer n (0 <= n <= 20).

Output

The factorial of n as a number.

Sample tests

Sample 1

Input: 5
Expected: 120

Your solution

Learning objective

Learn recursion with proper base cases and understand factorial computation.