Check out a free preview of the full TypeScript 5+ Fundamentals, v4 course

The "Dictionary Exercise" Lesson is part of the full, TypeScript 5+ Fundamentals, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Mike provides a challenge for students to write functions that mimic the behavior of map, filter, and reduce functions but for dictionaries instead of lists. Sample data and an interface for a dictionary are provided, and the goal is to write these functions so that they pass a test suite.

Preview
Close

Transcript from the "Dictionary Exercise" Lesson

[00:00:00]
>> Finally, I want to give you some further research for further learning if you'd like to choose to do it as homework or as an exercise that you can do in your own time. There's one last challenge at the end of the course. And it is taking what we've just used to define our listToDict generic function, to build map, filter, and reduce functions that apply to dictionaries.

[00:00:29]
You're probably familiar using these methods on arrays. And we're just going to create the dictionary equivalent because they're both collections. And so it's sort of the same concept, but instead of iterating through a list in order. We're going to iterate over key value pairs and you know, your map function will create a new dictionary with some transformation.

[00:00:52]
Your filter will filter through key value pairs, your reduce function will iterate over key value pairs. In a dictionary and give you a reduced value, and it's like the caller's decision with their reducer to define what that value is. So, here's the starting point code and this is just sample data.

[00:01:14]
But I'd like you to write these functions such that they would work for a variety of different inputs. I've given you a dictionary of fruits, that have color and mass. The mass is there for the reduce example, if you wanted to sort of sum up the mass of a bunch of different stuff.

[00:01:30]
That would be a great example of getting a single value out of a group of things. Here's an interface that describes a dictionary. We're using a type param here, so you could say this is a dictionary of fruits, or something like that, just by passing in T. And you can fill in these functions, so mapDict, filterDict, and reduceDict.

[00:01:55]
As usual, don't mess with stuff below this line. This is a test suite, you've got some positive and negative test cases. Here's an example of a negative test case. This should fail, but your goal would be to write those functions until all of these things pass. And there's a spoiler down here, if you click this, you'll see the solution.

[00:02:19]
So why don't you give this a shot? See how far you get, and then come back, compare with the solution, and see if we wrote the functions in the same way.

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