Complete the code to define a recursive function that counts down from n to 1.
function countdown(n: number): void {
if (n <= 0) return;
console.log(n);
countdown([1]);
}The function calls itself with n - 1 to count down until it reaches 0.
Complete the code to calculate factorial of n recursively.
function factorial(n: number): number {
if (n <= 1) return 1;
return n * factorial([1]);
}Factorial multiplies n by factorial of n-1 until it reaches 1.
Fix the error in the recursive Fibonacci function to correctly compute fib(n).
function fib(n: number): number {
if (n <= 1) return n;
return fib([1]) + fib(n - 2);
}The Fibonacci number is the sum of fib(n-1) and fib(n-2).
Fill both blanks to create a recursive function that sums numbers from 1 to n.
function sumTo(n: number): number {
if (n === 1) return 1;
return n [1] sumTo(n [2] 1);
}The function adds n to the sum of numbers from 1 to n-1.
Fill all three blanks to create a recursive function that reverses a string.
function reverse(str: string): string {
if (str.length <= 1) return str;
return reverse(str[1]) [2] str[3];
}The function calls reverse on the string without the first character, then adds the first character at the end.
