Replacing It All explainer


#1

The aim of this puzzle: Use the recursive updateAllNames function to replace all the typos.

Walkthrough of the solution: Running a recursive function is just like calling any other function. You use the function name and put arguments (input values) in the parentheses. The updateAllNames function needs 3 input values:

  • The main string that is being searched and edited

  • The oldPart which is a string that will be replaced in the main string

  • The newPart which is a string that will take the place of the oldPart inside the main string

In this puzzle, the main string is the travelDocument. The oldPart is the typo: 'grasshoper' with 1 ‘p’. And the newPart is the correctly spelled version: 'grasshopper'. If you look inside the function definition, you’ll see the command: string.replace(oldPart, newPart). This makes sense for the arguments we gave it: travelDocument.replace('grasshoper', 'grasshopper').

The function call will return the updated travelDocument, but you’ll need to put it inside of a console.log() to see the new value.

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: Calling Functions, Code Block (function, if statement), console.log(), Recursion, .replace()


#3

I dd exactly this but it still didn’t work


#4

Could you post a screenshot of your code?

–Frankie


#5

I have the same problem


#6

As an FYI, should you have the correct solution and you are still not able to get to the next puzzle, try clearing the cache of the app then reset the puzzle and insert the solution again.

Doing this I was able to proceed further.


#7

I have the same problem. Can’t get to the next puzzle with the right code.


#8

Could you post a screenshot of your code here?

–Frankie


#9


#10

Just a very small but important thing: there is an extra space at the end of the 'grasshopper' string.

Notice how where the closing quote mark is:
'grasshopper '
'grasshopper'

–Frankie


#11

Thanks Frankie. It went like yesterday!