Complete the code to make the function call itself recursively.
function factorial(n: number): number {
if (n <= 1) return 1;
return n * factorial([1]);
}The function calls itself with n - 1 to reduce the problem size until it reaches the base case.
Complete the code to correctly calculate the sum of numbers from 1 to n using recursion.
function sumToN(n: number): number {
if (n === 1) return 1;
return n + sumToN([1]);
}The function calls itself with n - 1 to add all numbers down to 1.
Fix the error in the recursive Fibonacci function to avoid infinite recursion.
function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci([1]) + fibonacci(n - 2);
}The function must call fibonacci with n - 1 and n - 2 to correctly compute the Fibonacci sequence.
Fill both blanks to create a recursive function that prints numbers from n down to 1.
function printDown(n: number): void {
if (n === 0) return;
console.log([1]);
printDown([2]);
}The function prints the current number n and then calls itself with n - 1 to count down.
Fill both blanks to create a recursive function that builds an array of numbers from 1 to n.
function buildArray(n: number): number[] {
if (n === 0) return [];
const arr = buildArray([1]);
arr.push([2]);
return arr;
}The function calls itself with n - 1 to build the smaller array, then adds n to the end. The base case returns an empty array when n is 0.