Functional-Light JavaScript, v3

Closure Exercise

Kyle Simpson

Kyle Simpson

You Don't Know JS
Functional-Light JavaScript, v3

Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Closure Exercise" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle instructs students to practice closure by building a string builder utility that makes use of previously closed over variables.

Preview
Close

Transcript from the "Closure Exercise" Lesson

[00:00:00]
>> Kyle Simpson: Let's do a practice exercise on this topic of closure. It feels like a really important topic. We're probably gonna keep coming against it over and over again in functional programming. It's good for us to get some more exposure to it. In this exercise, we're going to practice closure, by building a utility.

[00:00:15]
It may seem a little strange. We're building up a utility that we'll call stringBuilder. If you look at the exercise and specifically look at the test cases down at the bottom, you'll see what this is supposed to be able to do. We're supposed to be able to call this function, stringBuilder and pass it a single string.

[00:00:34]
And then if we call it again with another string and another string and another string, we can just keep adding more and more strings to it. Until we call it once without any input and then it's gonna go back and concatenate all of those strings together and give us one final string.

[00:00:54]
And also, not only will it do that, but we should be able to capture those individual specialized functions. So we should be able to capture for example, Hello, is the intermediate function, after stringBuilder was called with Hello. And we should be able to call this twice, producing two different functions, in this case Kyle and Susan and then we should be able to call Kyle again.

[00:01:19]
And when we call it with nothing, this question should come back as the actual string and greeting should come back as the actual string. Okay, this is a bit of a challenging exercise. A little bit of a hint can be seen in the read me, so you should definitely check out these hints.

[00:01:38]
It should be able to give you everything that you need, but it will be a little bit tricky and challenging. Make sure all of the test cases pass.

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