Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Factorial

A Closer Look at Functionseasy

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `factorial(n)` that returns `n!` (n factorial). Assume `n >= 0`. `factorial(0)` is `1`.

Sample tests

Input: factorial(0)
Output: 1
Input: factorial(1)
Output: 1
Input: factorial(5)
Output: 120

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • No base case causes infinite recursion.

Learning resources

  • MDN: Recursion (glossary)
Approach & explanation (try first)

Each call multiplies n by the factorial of n-1, bottoming out at 1.

Loading...
⌘/Ctrl + Enter

Run your code to see results.