Replacing It All explainer

Hello!
Tap on the blank where you have to type anything and then choose the ‘str’ button on the keyboard.
Hope that helps

Hello

I didn’t realise it had to be a string. Thank you.

i can’t understand anything of this code . what is the string here ? we all know that string is ’ ’ but where is the string here ??? and you all are calling it a string :sweat: :sweat: :sweat: and please help me :pray: :pray: :pray: and give please explain me it easily.

The ‘string’ in the code is just the parameter that means anything else can be put in there while calling the function.

The parameter is just named ‘string’ to make clear that a string has to be put in there.

Explanation of the code:
Part 1
function updateAllNames(string, oldPart, newPart)

This code takes a string, a word(that you think is a typo in the string) and another word (that you think is the correction of the typo)

Part 2

function updateAllNames(string, oldPart, newPart) {
if (string.includes(oldPart) === false) {
return string;

The above code takes the string and the oldPart (typo) and the if statement checks if the string contains the oldPart and if it doesn’t then that means the word doesn’t contain that typo and it simply returns the string.

Part 3

function updateAllNames(string, oldPart, newPart) {
if (string.includes(oldPart) === false) {
return string;
}
string = string.replace(oldPart, newPart)
return updateAllNames(string, oldPart, newPart)
}

If the string does contain the oldPart then that means the output of

if (string.includes(oldPart) === false)

Is false

Then the code outside the {} of if statement will be executed…

string = string.replace(oldPart, newPart)

This updates the string and replaces the oldPart(typo) with newPart(correction of typo)

Finally the code ends with the return statement

When the function is called…

console.log(updateAllNames(travel documents, ‘Grasshoper’ , ‘Grasshopper’ ));

(Here travelDocument is a variable which contains a string )

So… With this code, the word ‘Grasshoper’ is replaced with ‘Grasshopper’

My explanation might not explain everything and I might be wrong but I hope you understand.:grinning:

1 Like

I also did exactly the same thing, and still don´t working

Hey there, post a screenshot and I can take a look.

I cannot proceed, My code is the same as the sample solution, except for a semi-colon that I can’t remove that I think might be the root of my problems. It will not let me restart

nvm found my mistake typo grasshoppper lmao

1 Like

I did exactly this but it won’t work! :face_with_symbols_over_mouth:

1 Like

Code is

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'));

same thing! :rage:i do not get it !!!

1 Like

@Grasshopper_Frankie it helped a lot!

I somehow lost the thread of thought at this particular lesson.
Can someone please explain it to me where these string, oldPart and newPart variable are coming from? They are not defined anywhere.
And then I don’t understand how inside the updateAllNames function, we are calling the function with return. I also did not understand === false part. I don’t remember ===true/false anywhere before in the lessons. It seems like if else problem, why we are not using if else? Lastly, travelDocument is definitely a variable. Why we are calling it a string? I don’t understand the whole.

Code:

  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'));

I might be wrong, but this seems more clearer. Please correct me if I’m wrong.
My code:

var chat = "This massage has typo";
var fix = (a, b) => {
    if (chat.includes(a) === false) {
        return chat;
    }
    else {
        return chat.replace(a, b)
    }
}

console.log(chat);
console.log(fix('massage', 'message'));

Didn’t understand the part where the return statement is calling the function it is nested in.

Hey there, there’s 2 concepts to explain here, parameters and recursion.

string, oldPart, and newPart are the parameters of the function. They are added in the () of the function declaration. When the function is called, whatever arguments are passed in will be stored in those parameters.

Let’s look at a simpler example:

function add(num1, num2) {
    return num1 + num2;
}
add(5, 6);

num1 and num2 are parameters. 5 and 6 are arguments. When the function is called, the function gives num1 the value 5 and num2 the value 6, and then returns 5 + 6, which is 11.

Hope that clears things up.

As for recursion:

Remember that .replace() will only replace the first instance it sees (from left to right). So in your example, it will fix 'This massage has a typo', but it won’t fix 'This massage massage has a typo massage'.

This is why we use recursion, by making the function call itself over and over, each time fixing one mistake, until all the mistakes are fixed.

First, we need to tell the function when to stop running, otherwise it will call itself forever, and your program will go into an infinite loop and crash. That’s why we have that first check:

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

.includes returns true or false. Here we’re checking if it has returned false, and if so, the function returns the string and stops running.

However, if it returns true, then this if statement won’t run, and the code after it will run. This code will use .replace() to replace one mistake, and then call itself, which makes the function run again.

As the function is running again, the if statement checks if there’s any mistakes left. If there aren’t, it just returns the string, or else it fixes one more mistake and runs again. The function will keep calling itself as long as there are mistakes to fix.

You could use an if...else statement if you want, but it wouldn’t change the behavior of the function. In programming, as in life, there are often many ways to solve a problem, so it’s just a matter of preference.

Hope this helps! Let me know if you have any questions.
Ben

1 Like

are u still facin g the problemioo

Did anyone else take out the
“console.log(travelDocument)”
And then add the
“console.log(upDateAllnames(travelDocument, ‘grashoper’, ‘grasshopper’));”

Then it didn’t print out the mistakes but just the corrected copy!

Screenshot_20200530-073501_Grasshopper|281x500

there is an orthographic mistake, you need to write ¨grasshoper¨ instead of ¨grasshopper¨ on the first one

For all of you having the problem of it not working even if you type in the correct code, I restarted the device, reset the code, logged off the account and logged in again but the real solution is that for the UpdateAllNames, you have to write TravelDocument, then the string ‘grasshoper’ then the string ‘grasshopper’ THE SOLUTION IS THAT BOTH THE G’S IN THE BEGINNING OF GRASSHOPPER HAVE TO BE LOWERCASE, NOT UPPERCASE