A Practical Guide to Algorithms with JavaScript

# Basic Memoization Exercise

Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course:
The "Basic Memoization 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 use a global cache to memoize.

Get Free Access Now

Transcript from the "Basic Memoization Exercise" Lesson

[00:00:00]
>> Bianca: We're just gonna jump in. So, this is a three part exercise.
>> Bianca: Where we're going to cache. So my recommendation for going through this exercise is to click this button where it says open it and repl.it. Or copy and pasting into your ID where whatever your preferred ID maybe or your text editor.

[00:00:23] Open it and repl.it, maybe make an account and you can fork it into your own repl.it account. And then you can just have a copy of this for yourself. So our first task is to write a function called Times10. It takes an argument n and then multiplies n times 10.

[00:00:40] So here is an empty function for you to fill out. That's our very first task. And then the second thing that we need to do is we're going to cache, or menolize, our results. So you can see in our console log example that the first time we do menolize times 10 we calculate it, right?

[00:01:05] So we do 9 times 8, etc etc. Or sorry, 9 times 10 is the only calculation, we're not doing factorial yet.
>> Bianca: So just one calculation there. The second time we do it is the cache version. So we already have 90 saved in there. We don't have to recalculate 9 times 10.

[00:01:24] This is kind of a trivial example where the calculation isn't very expensive. However, hopefully you can see the larger picture implications of this caching. When the operation is very expensive, like factorial where you have this chain of multiplication that you have to go through.
>> Bianca: Okay, are you guys ready?

[00:01:49] Any questions about the task? First thing we're just gonna write our function Times10, get a little warm up, warm up our fingers a little bit. Then, we are going to write memoTimes10. And it's gonna reference this Times10 function, and whenever it has a result we're gonna cache it.

>> Bianca: Do you have a question, Kevin? Are you sure?
>> Speaker 2: I just have to look at it longer.
>> Bianca: Okay.
>> Speaker 2: It just looks a little overwhelming at first.
>> Bianca: Yeah, it's mostly my pro tips. So there's some tips in there.

[00:02:32]
>> Bianca: So the first tip is to create a function that checks if the value for n has been calculated before.
>> Bianca: So here it is, you could call it memoTimes10. And then inside of that function, if the value for n has not been calculated, then calculate it and save the result in the cache object.

[00:02:56] So the next time you visit it, you can check if it's already been calculated. If it's already been calculated, then just return that value versus actually calculating it. So there's an if conditional in there that says has it been seen already or has it not been seen already.

[00:03:13] Very similar to your bread crumbs example where, had we seen that value before or had we not seen that value before?
>> Bianca: So I'm just gonna let you guys take a stab at it.