JavaScriptEasyNumbers10 min
Compute a factorial
Compute n factorial, with 0 factorial defined as 1. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess accumulate the product iteratively.
Input
A JSON integer n.
Output
Print one compact JSON value: an integer.
Constraints
0 <= n <= 18; all results are exact JavaScript integers.
Example
Input
5
Output
120
Example 2 input
2
Output
2
Boundary case: Zero factorial is one
Public test cases (2)
Input
5
Expected output
120
Input
3
Expected output
6
Hints (2)
- Start the product at one
- Zero factorial is one
Target complexity: time O(n) · space O(1)
