0
0
DSA Typescriptprogramming~10 mins

Factorial Using Recursion 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.

DSA Typescript
function factorial(n: number): number {
  if (n === 0) {
    return [1];
  }
  return n * factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
An
B0
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for the base case.
Not handling the base case at all.
2fill in blank
medium

Complete the recursive call to calculate factorial of 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
Cn + 1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 which causes infinite recursion.
Using n which does not reduce the problem.
3fill in blank
hard

Fix the error in the base case condition to correctly check for n equals zero.

DSA Typescript
function factorial(n: number): number {
  if (n [1] 0) {
    return 1;
  }
  return n * factorial(n - 1);
}
Drag options to blanks, or click blank then click option'
A==
B===
C=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using = which assigns instead of compares.
Using != which checks for inequality.
4fill in blank
hard

Fill both blanks to create a recursive factorial function with a base case and recursive step.

DSA Typescript
function factorial([1]: number): number {
  if ([2] === 0) {
    return 1;
  }
  return [2] * factorial([2] - 1);
}
Drag options to blanks, or click blank then click option'
An
Bnum
Cx
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing reference errors.
Using undefined variables inside the function.
5fill in blank
hard

Fill all three blanks to create a recursive factorial function with parameter, base case check, and recursive call.

DSA Typescript
function factorial([1]: number): number {
  if ([2] === 0) {
    return 1;
  }
  return [3] * factorial([3] - 1);
}
Drag options to blanks, or click blank then click option'
Anum
Bn
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names causing errors.
Forgetting to decrement the variable in recursive call.