You might also get a task like this creative function to replace parameters in your realm.
And actually, this is a real world task because here we have an initial euro like it is generated in
all rounders for all frontend frameworks.
And in this case, we have like initial euro less both SLAs and here Colin Powell's posted slash common
slash column Commentated, which means this two parameters with Collins will be replaced by ads at some
point later.
And actually, we must try to function replace params in the URL where we can throw inside the URL with
such structure and our array of replacements.
And as you can see inside, every single object we must provide from and tool, for example, from Paul's
2D means that we must replace Colin posted with a do and here commentator with a three.
And of course, this function must be generic.
It is not only for posts and comments, but any replacements inside the URL.
And actually, before we will start, Solomon, I want to show you one more thing how you can think
about solving problems.
For example, you can write something like To-Do list and in this case were trying to think in steps
regarding what we want to implement.
For example, what will be the first step here?
As you see, we have an array of our replacements, which actually means we have a function of with
two arguments.
And actually, we want to move through every single replacement and to play this replacement to our
as well.
So actually, here we can write go through replacements.
And the second point will be maybe replace parts of each.
The replacements and the last thing will be here, return in your scene.
And this list in this case might sound like an obvious thing, but it can help if you write something
like this to implement more complex logic and actually directly want to implement a correct solution.
And we can use reviews for this because it's huge.
Our code best.
But I also want to show you the single problem that you might don't actually hear but want to create
our function, replace programs in general.
And we're getting here, you as well and our list of replacements.
And actually, what we want to do here is reduce for our replacements.
So replacements door to reduce.
And here we have a function with accumulator and each of replacement.
And this initial value we have here all year round because this is what we want to change now here inside
the function, we must return updated strain.
And for this, we can use a plain JavaScript function, which is called replace.
And actually, here you might write it.
Run like this.
You can think, OK, I have a URL.
We can simply right here.
Your cell equals your cell to replace.
And here we are.
Place and first a full column plus replacement not from.
And we're changing it to a replacement DOD tool.
This is it.
And after this, we want to return our URL.
And actually, this code looks kind of correct.
And now I want to move our initial URL and results here to the bottom and right console.log.
Result yourself.
And let's check what we have.
AMD allowed in the page and we'll get an exact list less post slash one slash common slash three, which
actually means this logic was reduced, is working correctly so well.
We've been through every single replacement.
Here we updated our euro and here return it and all looks good.
But actually, you have a huge problem.
As you can see, my accumulator variable is not highlighted, which actually means we're not using here
reduced correctly and we're not using accumulator at all.
What we're using, we're using this URL from params and we directly mutate this hotel every single time.
And actually, this is completely wrong.
First of all, because we should not mutate our programs.
And secondly, the most important, but we're using here reduce like a for loop because actually, we
simply mutate our variable from outside in the loop and we're not using our accumulator.
Actually, the correct code here is not here to replace, but simply return accumulator replace.
Because as you can see, we're provided your role here as an initial value and we should not use URL
here inside.
We don't want to change our URL.
We want every single time to update our accumulator and return in your accumulator.
And as you can see, this code is working in exactly the same, but this is completely different.
Previously, if you would write code like this, this is completely wrong on the interview because you
are not using reproduce, like reduce.
But just like for loop, the same goes with map.
If you are using the map just to look through things, you are doing it itron.
With Map, you need to transform your array and return it back.