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

The "Basic Memoization Solution" 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:

Bianca walks through the Basic Memoization Exercise and answers questions from students.

Preview
Close

Transcript from the "Basic Memoization Solution" Lesson

[00:00:00]
>> Bianca: We had two tasks. We were writing a function that takes n, multiplies n times 10. Here is our solution for that, n times 10. Our second one was building on that first one where we're going to use an object to cache the results of our times 10 function.

[00:00:23]
So how do we do that? So we check if n is in cache. If it is, I have a handy console.log just for reference here. We're gonna just return that value. If not, we're going to say that we're calculating the results.
>> Bianca: And we're gonna calculate it here, right?

[00:00:45]
This is our calculation. We're calling our function here versus here, we're just looking it up in our cache. If it's not already in our cache we're gonna call that function, and we're going to save the results of that into our cache. And this is important. We're also gonna return that result so that when we're interacting with this, it's actually returning 90 every time because that's actually what we're interested in.

[00:01:11]
When we're calling this method and we're consuming the method, we want to get the result. So don't forget to return that result. So,
>> Bianca: The first time we call it with 9, it's gonna be calculated. The second time it's gonna hit this condition and it's just gonna do a property lookup in our cache.

[00:01:34]
Any questions about this solution?
>> Speaker 2: So there's something that's still not quite clicking with this whole. So I understand the caches and the object, right? So when we did the first function, it's something in the background that's happening, hey, I've calculated this, right?
>> Bianca: Mm-hm.
>> Speaker 2: And it's saved somewhere like in this abstract object.

[00:01:53]
>> Bianca: Well, it's happening right here.
>> Speaker 2: Because how do we, I guess, tie the two together? When we say on line 15, if n is in cache, how does that function even knows about cache, because it's just a variable before it? I know it's kind of a bridge, but I don't know how the two intersect.

[00:02:10]
>> Bianca: Got it. So in this example, we're using this global cache that lives in a parent scope, and functions can reach up into the parent scope, mutate data. It's not a best practice, but it's possible and we're gonna make it better in the next exercise. So I think the thing that's a little bit confusing to people is, instead of starting at the first if condition, let's start at the else, okay?

[00:02:37]
So we're gonna call memo times 10 with 9. So memo times 10. Whenever we call a function, we're gonna hop into that body of the function. n is nine. And n is not in cache, right? Cache is empty at this point. It's initialized as empty. So we run into this else.

[00:02:55]
The first thing that we're gonna do is we're gonna calculate times 10 of n which is 90. We're gonna save that n to results. And here's where the magic happens. We're saving cache of n so cache of 9 equals 90, right? Before we were saving our values to true because we were just checking if we had seen it.

[00:03:15]
In this case, we are saving the results of that calculation. So we're doing the calculation, n times 10 is 90, so our result is 90. And we're saving that n to cache of n. What is n again? 9. So and then we're returning the result which is 90, the thing that we're actually interested in as a consumer of this function.

[00:03:40]
Okay, so that's the first time. Let's go to the second time and then I'll take your question. I think this might answer your question. The second time n is 9, and then we're gonna say, if n is in cache, n isn't, so n is 9, is in cache and what is the value that is saved?

[00:03:57]
>> Speaker 2: 90.
>> Bianca: 90, so in this cache we have something that looks like 9, 90. So when we look up 9, it exists, great. And then we just return a cache of n, which is 9. And it's gonna return the value associated with 9, which is 90.
>> Bianca: Questions?

[00:04:24]
>> Speaker 2: Why are we declaring cache as outside of the scope of memo times ten function?
>> Bianca: Because the second time that we call it, we just need to retain access to it. If we initialized it here in the way that this is written, every time we recall memo wise, it's gonna read calculate the cache as empty.

[00:04:49]
So we need to save it somewhere where it's not gonna get really overwritten because we're gonna initialize it as empty, and then we're gonna write to it. And the next time we call it, we execute the body of the function. We're gonna execute the body of the function, I'm just gonna rewrite that cache to be empty.

[00:05:09]
But that's a really good question, all right.
>> Speaker 3: Whenever pushing to the cache, is that what cache with n equals result, is that what it's essentially doing?
>> Bianca: Yeah, yeah, that's exactly what it's doing. So it's saying okay, cache, create a property called 9, and save the value 90.

[00:05:31]
>> Speaker 3: Okay.
>> Bianca: Mm-hm.
>> Bianca: And the reason that we're using the bracket notation instead of cache.in, because in is a variable name. And so when you were gonna save a property to an object, that's a variable name, you need to put it inside the brackets. And it's going to evaluate what is n, and the environment is gonna say n is 9.

[00:05:56]
Just like, we're gonna evaluate what is the result? The environment's gonna say the result is 90 in this case.

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