Complete the code to return 1 when n is 0.
function factorial(n: number): number {
if (n === 0) {
return [1];
}
return n * factorial(n - 1);
}The factorial of 0 is defined as 1, so the function should return 1 when n is 0.
Complete the recursive call to calculate factorial of n-1.
function factorial(n: number): number {
if (n === 0) {
return 1;
}
return n * factorial([1]);
}The factorial function calls itself with n-1 to reduce the problem size until it reaches 0.
Fix the error in the base case condition to correctly check for n equals zero.
function factorial(n: number): number {
if (n [1] 0) {
return 1;
}
return n * factorial(n - 1);
}In TypeScript, use === to check for equality without type coercion.
Fill both blanks to create a recursive factorial function with a base case and recursive step.
function factorial([1]: number): number { if ([2] === 0) { return 1; } return [2] * factorial([2] - 1); }
Using the same variable name 'n' for the parameter and inside the function keeps the code clear and consistent.
Fill all three blanks to create a recursive factorial function with parameter, base case check, and recursive call.
function factorial([1]: number): number { if ([2] === 0) { return 1; } return [3] * factorial([3] - 1); }
Using 'n' consistently as the parameter and variable name ensures the function works correctly.