Complete the code to define a function that calls itself.
function recurse() {
console.log('Hello');
[1]();
}
recurse();The function calls itself by its name recurse(), which is how recursion works.
Complete the code to add a base case that stops recursion when n is 0.
function countdown(n) {
if (n [1] 0) {
return;
}
console.log(n);
countdown(n - 1);
}
countdown(3);The base case checks if n == 0 to stop the recursion.
Fix the error in the recursive function to avoid infinite recursion.
function sum(n) { if (n <= 0) { return 0; } return n + sum([1]); } console.log(sum(3));
To reduce n each time and reach the base case, we call sum(n - 1).
Fill both blanks to create a recursive factorial function with a base case.
function factorial(n) {
if (n [1] 1) {
return 1;
}
return n * factorial([2]);
}
console.log(factorial(4));The base case stops when n is less or equal to 1, and the recursive call uses n - 1.
Fill all three blanks to create a recursive function that counts down and stops at zero.
function countDown(n) {
if (n [1] 0) {
return;
}
console.log([2]);
countDown([3]);
}
countDown(3);The base case checks if n is greater than zero, prints n, then calls countDown with n - 1.
