Question Bank

JavaScript Factorial: Three Implementations Quiz

Difficulty: Medium

Three seeded ways to compute n! (iterative loop, classic recursion, reduce), plus two companions on memoization and on guarding against negative inputs.

Question Bank
/

JavaScript Factorial: Three Implementations Quiz

JavaScript Factorial: Three Implementations Quiz

Three seeded ways to compute n! (iterative loop, classic recursion, reduce), plus two companions on memoization and on guarding against negative inputs.

Question Bank
Medium
JavaScript
5 questions
quiz
recursion
math
interview-prep

298 views

3

Implement factorial(n) using a for loop. Return 1 for n <= 1 and the product of integers from 2 to n otherwise.

Examples

Example 1:

Input: factorial(0)
Output: 1
Explanation: 0! is defined as 1.

Example 2:

Input: factorial(5)
Output: 120
Explanation: 2 * 3 * 4 * 5 = 120.