A Practical Guide to Algorithms with JavaScript

Generic Memoize Function Exercise

A Practical Guide to Algorithms with JavaScript

Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course

The "Generic Memoize Function Exercise" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

In this exercise, students make a memoize function generic by passing in the function rather than hardcoding a function.

Preview
Close

Transcript from the "Generic Memoize Function Exercise" Lesson

[00:00:00]
>> Bianca: Well, I'm going to now talk about our third prompts. So we're gonna take the solution from this then we're gonna build on it a little bit more.
>> Bianca: So we're gonna make our memo function more generic. And let's take a look. So previously, we were either referencing the function outside of the scope, the times10 function outside of the scope.

[00:00:29]
We were calling times10 n inside the body of our function. And a second example, we just did the work directly inside of the function, we said 10 times n. In this case, we want to enable this function to take any function as a callback, and that is going to be the function that calculates a result.

[00:00:55]
So whatever this callback calculates with some value and/or could be multiple depending on how you wanna do it but we can stick with one parameter. It's a little bit simpler to get started with. But you can also make it marginal and it can pass many arguments. So essentially, we need to use a closure.

[00:01:20]
Again, we're gonna return a function. And in that step where we're calculating the result, that's where we're gonna call our callback, which is just a generic function that's gonna do some calculation for us. In our case, our callback is just gonna be the times10. But you could imagine that it could be really anything.

[00:01:42]
>> Bianca: Any questions? I feel like there's questions.
>> Bianca: Okay, so let's take a look at the other solution. And we can talk about.
>> Bianca: So our solution, here, so our very first solution, we are referencing the times10 function in here. But it's looking up in the parent scope and it's grabbing it.

[00:02:17]
So it's like grabbing out, right? We don't like to do that too much, similarly with the cache. Outside of those scope we're reaching out of our function. We wanna tidy it up a little bit. So what do we do? We're gonna move our cache inside of our function.

[00:02:31]
So we did that last time. We did something a little bit different. We could still, for the sake of consistency, we could still reference this outside. So the next step is how do we make this generic, so that instead of always doing times10 we could do times11, right?

[00:02:54]
Being passed in as an argument,
>> Bianca: Up here. So you pass something here, and I call it CB for call back, call back is just a function that is passed into a function then called inside of that function. And so we need to figure out how to make this little more generic using callback versus using our times10 function that lives outside.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now