0
0
DSA Typescriptprogramming~10 mins

Why Recursion Exists and What Loops Cannot Express Cleanly in DSA Typescript - Test Your Knowledge

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a recursive function that counts down from n to 1.

DSA Typescript
function countdown(n: number): void {
  if (n <= 0) return;
  console.log(n);
  countdown([1]);
}
Drag options to blanks, or click blank then click option'
An - 1
Bn + 1
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n does not change the argument, causing infinite recursion.
2fill in blank
medium

Complete the code to calculate factorial of n recursively.

DSA Typescript
function factorial(n: number): number {
  if (n <= 1) return 1;
  return n * factorial([1]);
}
Drag options to blanks, or click blank then click option'
An
B1
Cn + 1
Dn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Returning n without recursion misses the factorial calculation.
3fill in blank
hard

Fix the error in the recursive Fibonacci function to correctly compute fib(n).

DSA Typescript
function fib(n: number): number {
  if (n <= 1) return n;
  return fib([1]) + fib(n - 2);
}
Drag options to blanks, or click blank then click option'
An
Bn - 1
Cn + 1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n or 1 as argument does not compute Fibonacci correctly.
4fill in blank
hard

Fill both blanks to create a recursive function that sums numbers from 1 to n.

DSA Typescript
function sumTo(n: number): number {
  if (n === 1) return 1;
  return n [1] sumTo(n [2] 1);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition.
Not decreasing n causes infinite recursion.
5fill in blank
hard

Fill all three blanks to create a recursive function that reverses a string.

DSA Typescript
function reverse(str: string): string {
  if (str.length <= 1) return str;
  return reverse(str[1]) [2] str[3];
}
Drag options to blanks, or click blank then click option'
A.slice(1)
B+
C[0]
D.slice(0, 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect slice indices.
Using multiplication instead of concatenation.