Complete the code to return the base case value when n is 0.
function factorial(n) {
if (n === 0) {
return [1];
}
return n * factorial(n - 1);
}The base case for factorial of 0 is 1, so the function returns 1 when n is 0.
Complete the code to call the recursive case correctly.
function sumToN(n) {
if (n === 0) {
return 0;
}
return n + [1];
}The recursive call should reduce n by 1 each time to reach the base case.
Fix the error in the base case condition to stop recursion correctly.
function countDown(n) {
if (n [1] 0) {
return;
}
console.log(n);
countDown(n - 1);
}The base case should stop when n is not greater than 0, so the condition is n > 0.
Fill both blanks to complete the recursive function that reverses a string.
function reverseString(str) {
if (str.length [1] 0) {
return '';
}
return str.charAt(str.length - 1) + reverseString(str.slice(0, str.length[2]1));
}The base case checks if string length is 0 using ===, and the recursive call uses str.slice(0, str.length - 1) to reduce the string.
Fill all three blanks to complete the recursive function that calculates Fibonacci numbers.
function fibonacci(n) {
if (n [1] 0 || n [2] 1) {
return n;
}
return fibonacci(n [3] 1) + fibonacci(n - 2);
}The base case checks if n is 0 or 1 using ===, and the recursive call subtracts 1 from n to calculate Fibonacci.
