You might also get a question about memorization.
And here's your case consumer must design the memorization function, which adds 10 to provide value
and take it from cash if it was already calculated.
And if you don't know what term memorization in JavaScript means, it means that vacation values to
avoid making the same operation again, which actually means, for example, would want to subtract
two numbers.
And if we did it previously and we already know the answer, then we must just return this answer and
not calculate it again.
Obviously, it doesn't make sense to make it with subtract or some, but it makes a lot of sense when
we have some difficult calculations.
This is where Hillary must build an example of memorization function.
And actually, it's not that difficult.
So here I want to create a function, memorize ad and then say this function of a don't need any arguments,
but we want to create a cache inside.
This is why here I will create cache property and this is just an object.
Now here I want to return in your function, and you can see that this is a closure because actually
we can start on with some variables when we have a closure.
This is why here I will return the function with value.
And now here is how we're using it outside.
So here we want to create new add function and we just call him lies.
So in this case, new add is a function and we can use it.
This is why here we can write new add, for example.
Nine And this is our value and in this case will be output 19 calculated by 19.
Because actually, as you can see in our future, it must add 10 to our provided value.
So we're throwing inside nine and we're getting back nine 10.
But actually, when we calling you add with nine again, the output will be also 19, but it should
be taken from cat.
It should not be calculated again.
And this is exactly what memorization is doing.
So now we just need to implement this memorization logic.
So here we can write if we have a value in cash.
So we're checking if we have such value inside our object, then we want to just return it.
And here I want to write console.log so you can see that it is minimized.
So here will be fetching from cash.
And after this, I want to write return cash.
And here is our value.
So as you can see, we don't do any calculations here.
We simply return our value and we also have else in here.
We have calculations.
So we're right in here calculating results.
And after this, we must really calculate it.
So let's create here a value, for example, result and we want to put higher value plus 10.
And after this, we must put it in cash before we return it.
In this case were stolen.
Our result for the next right.
This is why here we can write cash value equals result in this case, where I sign in cash for our number
nine.
In this case, win win next time right here.
Nine, Then we're getting the value from cash.
And after this, we can simply return our result.
And this is just the value that we calculated here.
Let's check this out.
I'm reloading the page as you can see where I get and calculating results.
This is our first try here and calculated 19, which actually means we really calculated something.
But the second time this call we have fetching from cast nine 10, which actually means we didn't calculate
this time this number, but we took it from cash.
And this is how you typically implement memorization.
You can get different tasks, but the idea is the same.
You have some cash, you have a closure and you must deliver the value from the cash and put it in the
cash on the first calculation.