Test Your Knowledge 4

Let's take a small quiz!

We'll cover the following...
...

This Quiz will take approximately 10 minutes.

1

The code below replaces all occurrences of substring b with substring a in string. This process is done recursively.

function replace(string, a, b) {
  // Base case
  if (string.length == 0) {
    return "";
  }
  
  // Recursive case
  ______________________________________________
}

What should the recursive case of the following code be?

A)
  else if (string.substring(0, b.length) == b) {
    return a + replace(string.substring(b.length), a, b);
  }
    
  else {
    return string[0] + replace(string.substring(1), a, b);
  }
B)
  else {
    return string[0] + replace(string.substring(1), a, b);
  }
C)
replace(string, a - 1, b - 1);
D)
  else if (string == b) {
    return replace(string.substring(b.length), a, b);
  }
    
  else {
    return string[0] + replace(string.substring(1), a, b);
  }
Question 1 of 40 attempted
...
Access this course and 1400+ top-rated courses and projects.