0
0
DSA Typescriptprogramming~10 mins

Recursion Base Case and Recursive Case in DSA Typescript - Interactive Practice

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

Complete the code to return 1 when n is 0, the base case of recursion.

DSA Typescript
function factorial(n: number): number {
  if (n === [1]) {
    return 1;
  }
  return n * factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
An
B0
C-1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as the base case condition.
2fill in blank
medium

Complete the code to call factorial recursively with n-1.

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

Fix the error in the base case condition to stop infinite recursion.

DSA Typescript
function sumToN(n: number): number {
  if (n [1] 0) {
    return 0;
  }
  return n + sumToN(n - 1);
}
Drag options to blanks, or click blank then click option'
A==
B<=
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > causes wrong base case detection.
4fill in blank
hard

Fill both blanks to create a recursive function that counts down to zero.

DSA Typescript
function countdown(n: number): void {
  if (n [1] 0) {
    console.log('Done');
    return;
  }
  console.log(n);
  countdown(n [2] 1);
}
Drag options to blanks, or click blank then click option'
A===
B>
C-
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > incorrectly in base case or recursive call.
5fill in blank
hard

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

DSA Typescript
function sumNumbers(n: number): number {
  if (n [1] 1) {
    return 1;
  }
  return n [2] sumNumbers(n [3] 1);
}
Drag options to blanks, or click blank then click option'
A===
B+
C-
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of === in base case.
Using subtraction instead of addition in return.