Recursion Replacement explainer

The aim of this puzzle: Complete the recursive updateAllNames function.

Walkthrough of the solution: The updateAllNames function definition isn’t correct. It only replaces the 1st typo which is the same thing the regular .replace() method does. We need to make the function replace all the typos (spelling errors).

The 1st part of the function is an If Statement that checks if the string does not include any typos. This is the same as asking if all the typos are gone. If there are no typos, then we should just return the string because all the work is done. If that check fails, then we still have typos to fix.

The next line does a .replace() to fix the first typo it finds. Now our string is a little better off, because it has 1 less typo. But there may still be typos left. We should do something to check if we’re done or if there are any more typos to fix. Amazingly, we have a function that already checks if the string is free of typos otherwise it fixes 1 typo – the updateAllNames function that we’re looking at!

After we did our .replace() we should call the updateAllNames() function and return that value: return updateAllNames(string, oldPart, newPart); This almost like a loop, because when we run updateAllNames() the very 1st time, it will probably ask itself to run again, and again. But the important part is that each time the function is called, there is a check right away to see if the work is done. That’s called a “base case”, this is the case that we don’t need to recurse (make the function call itself) anymore.

Also, every time we recurse, we’re getting closer to the base case (no typos), because we’re removing 1 typo every time. Eventually, we will get rid of all the typos and the recursion is complete.

Sample code solution:
(Tap below to reveal)

function updateAllNames(string, oldPart, newPart) {
  if (string.includes(oldPart) === false) {
    return string;
  }
  string = string.replace(oldPart, newPart);
  return updateAllNames(string, oldPart, newPart);
}
console.log(travelDocument);
console.log(updateAllNames(travelDocument, 'grasshoper', 'grasshopper'));

JavaScript Concepts: Code Block (function, if statement), console.log(), Recursion, .replace()
Additional Code (hidden code that runs before the puzzle’s code):

let travelDocument = 
`Hello grasshoper${pickRandom(['','Travel','Passport','Ticket','Plans','Vacation','Holiday','Trip'])}!
You will be going to ${pickRandom(['New Zealand','Cyprus','the Maldives','Oahu','Palawan','Bali','Maui'])}.
Your travel code is: "${pickRandom(['grasshoper8ugs','im1grasshoper','no1grasshoper'])}"
Enjoy your trip grasshoper!`;
1 Like

There’s a typo :wink: replace ‘becuase’ by ‘because’

2 Likes

Can someone explain why an if…else isn’t used? When the code hits the first return, does it just return that thing? Why doesn’t the second return here make it go on forever?

1 Like

Hey there, this code would work with an if…else, but it also works equally as well with just a regular if statement.

Recursive functions need an exit condition to make sure they don’t run forever. That’s what this if statement is doing:

if (string.includes(oldPart) === false) {
  return string;
}

If there are no more typos left to replace, then string.includes(oldPart) === false will be true, and the code inside the if statement will run. This return statement will only run if there are no more typos to fix. Once a return statement is executed, a function stops running, so none of the code will run.

If there are still typos to fix, then the code inside the if statement will not run. The function will instead fix 1 typo, and then call itself again.

Once there are no more typos, the if statement will be able to run the return string, and the function will stop running.

– Ben

3 Likes

4 posts were merged into an existing topic: Recursion Replacement definition

There was definitely something wrong with my puzzle on this one. I entered the code and multiple times it told me I was wrong. When I finally chose to reveal answer it showed me the exact same code I was editing in, and it kept rejecting. I was really disappointed because I knew I had it right and now I have to start all over on my streak of puzzles answered correctly in a row. I wish I would have taken a screenshot but I didn’t know about this forum yet.

2 Likes

Hi,

Curious when you would want to use a recursive function vs. “for (let i = 0; i < array.length; i++)” vs. a “for (let element of array)” loop"?

Is a recursive loop an easier way of writing a for loop? Or do they serve other purposes?

2 Likes

It’s a Cinderella Story:
(or could be…)

“console.log(updateAllNames(travelDocument, Grashoper, GlassSlipper));”

1 Like

The last code doesn’t let me put the return string

I still don’t understand how does the ===false work in the if statement…

Can we replace more than 1 word with a typo, for instance
‘grasshoper’ with ‘grasshopper’ and ‘holidey’ with ‘holiday’?

2 Likes

Hi! The statement seeks to print the string without any typos. string.includes(oldPart) implies the ‘string with a typo(oldPart)’. Now saying that this statement === false means to say that the string doesn’t have a typo. So, if this statement above is true and the string is free of typos, you’ll see that it calls for the typo-less string to be returned.

Getting rid of the typos is the job for the ‘replace’ stuff. The if statement just wants to return the string if and when it’s free of typos.

umm

1 Like

i dont understand this one

Anyone Understand the Topic help me?

2 Likes

please, put the hidden code in the app. i didn’t understand it without the hidden code

Hope this helps!

Can someone please explain to me where did this come from “updateAllNames(string, oldPart, newPart);”
What is “oldPart”?
Why “return string;”?


:slight_smile: See. (if youAreStillThere ) lookAtMyScreenshot;

The hidden code is to long.


This is the other part.