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!


#12

Where can I find the full code, so I understand it better


#13

I dont have a .log or .return in my keys and it womt let me type it in. How do i get .log for console


#14

There should be a .log() button if you scroll to the right in the keyboard. There isn’t a .return key, but the 2nd key should be return, without the dot.

–Frankie


#15

I cannot do this thing. I will send a screenshot. Can you please help me do this.


#16

At the bottom of your code, the updateAllNames() function takes 3 arguments: the document, the misspelled word, and the correct spelling. You have the correct spelling twice. Change the 1st 'grasshopper' into 'grasshoper' by removing 1 letter 'p'.

–Frankie


#17

Thanks for the help. Is grasshopper good to children of 13 years.


#18


I have some problem,
Some body help.


#20

yeah, i tried to restart my device and stuff, but there is still not .log() or return.


#21

I rebooted my device and now i have the keys i need but now i am confused on what code i entered wrong Screenshot_20190211-151817_Grasshopper|281x500


#22

I’m not able to see the file you uploaded. But to solve this puzzle, there’s only a few things to do:

  • Reset, to make sure none of the starter code was changed
  • The code you add will go at the very bottom
  • Add console.log()
  • Inside the parentheses of .log(), add updateAllNames()
  • Inside the parentheses of updateAllNames(), you will add 3 arguments
  • The order of the arguments are:
    • The travelDocument variable
    • The 'grasshoper' string, with 1 'p', no capitals or spaces
    • The 'grasshopper' string, with 2 'p's, no capitals or spaces

–Frankie