Complete the code to return 1 when n is 0.
function factorial(n) {
if (n === [1]) {
return 1;
}
return n * factorial(n - 1);
}The factorial of 0 is defined as 1, so we check if n is 0 to return 1.
Complete the recursive call to calculate factorial of n-1.
function factorial(n) {
if (n === 0) {
return 1;
}
return n * factorial([1]);
}To calculate factorial recursively, multiply n by factorial of n-1.
Fix the error in the base case condition to correctly check for zero.
function factorial(n) {
if (n === [1]) {
return 1;
}
return n * factorial(n - 1);
}The base case must check if n equals 0 using '===' operator, not assignment '='.
Fill both blanks to complete the factorial function with base case and recursive call.
function factorial(n) {
if (n [1] 0) {
return 1;
}
return n [2] factorial(n - 1);
}The base case uses strict equality '===' to check if n is 0, and the recursive call multiplies n by factorial(n-1).
Fill all three blanks to create a factorial function with base case, recursive call, and correct return.
function factorial([1]) { if ([2] === 0) { return 1; } return [3] * factorial([2] - 1); }
The function parameter is 'num'. The base case checks if 'num' is 0. The recursive call multiplies 'num' by factorial of 'num - 1'.
