This course has been updated! We now recommend you take the JavaScript: From First Steps to Professional course.

Check out a free preview of the full JavaScript: From Fundamentals to Functional JS course:
The "Scope Exercise" Lesson is part of the full, JavaScript: From Fundamentals to Functional JS course featured in this preview video. Here's what you'd learn in this lesson:

The exercises in this part are all test-driven. Bianca spends a few minutes explaining how he exercise files are organized and then gets the audience started on the first exercise on scope.

Get Unlimited Access Now

Transcript from the "Scope Exercise" Lesson

>> [MUSIC]

>> Bianca Gandolfo: So I have the exercise. We go to the very first slide, we have the which is func-exercises?
>> Speaker 2: There's a question I'm not sure if do you need the var? And then you said the 4 inblock.
>> Bianca Gandolfo: The question is, do you need var? No you don't, I didn't want to give it away though.

[00:00:35] So, the question is do we need var. I can't delete that but do we need var, this var? No you don't.
>> Speaker 3: That a red herring?
>> Bianca Gandolfo: What's that?
>> Speaker 3: If you put it there it's not gonna matter, yeah.
>> Speaker 3: The last stuff you could touch on a lot fewer?

>> Bianca Gandolfo: Basically-
>> Speaker 3: Will we cover let in varied scope and let in the advanced JavaScript course on what matters?
>> Bianca Gandolfo: Yeah.
>> Speaker 3: So if you don't.
>> Bianca Gandolfo: Yeah, basically what let, it's just like instead of using var, you could say I can't edit this. Instead of var here, you would say let i = 0, then i would be in that block.

[00:01:28] I've never used it before, I don't have any experience with it, but that's the idea. If you take the advanced JavaScript course it'll go more in depth. On that and more in depth on scope. There's lots of aspects to scope and you can really dive deep into the mechanics of how the JavaScript engine works and how it parses it and goes through and more in depth.

[00:01:54] And it's really, really interesting and it will really inform your understanding of how JavaScript's really working in the background the way I do it is just kind of willy nilly kind of just the basic idea of how its run but there's a lot more too it actually. Cool.

[00:02:11] So here is a link for the function exercises and we're gonna start with scope This is a little different then the exercises that we did yesterday. The scope exercises are test driven. I have a dock here, the read me, that will explain exactly how to do it. But, the basic idea is, let me see if I have it open, No.

[00:02:39] The basic idea is if we go here, is that you're going to, you can copy this. You can clone down the repo, if you're familiar with that, or you can just download the zip once you download the zip, you can go into the scope folder. You open your SpecRunner in your browser, and that's gonna show you a bunch of failing tests.

[00:03:06] I'll just do it, let me,
>> Bianca Gandolfo: I'll just show you how to do it, let's see.
>> Bianca Gandolfo: Okay
>> Bianca Gandolfo: And then let's see. So I just download it. And you unzip it, scope-exercises.
>> Bianca Gandolfo: Where'd it go? Here it is so, inside there we have our spec runner which we open in the browser.

[00:03:45] And you can see that we have one pass and eleven failures.
>> Bianca Gandolfo: And then, let's see here. Then we want to open up this function.js. And you're going to open it with your text editor. And here we have some where our failing tests are see if I can.

[00:04:22] So we have this describe block which is gonna encapsulate all of our tests. And you see that we're running it immediately down there at the bottom.
>> Bianca Gandolfo: And then inside of the callback function here. We have before each, so before every it block that's before each is gonna be run.

[00:04:50] And we have this var actual variable that is initialize on the find. And so before each is run actuals gonna be set to null. And then, instead of this it block, we're going to explore the rule that this string describes. So this one says, a function has access to it's own local skull variables.

[00:05:11] And in this call back function, we are gonna be testing that. So here, we have this function. And then we have a variable named inner. We set ACTUAL to name, we call that function, and then we have this expect statement. So it's gonna be expect(ACTUAL === 'inner') So we want this to, this conditional here to be true.

[00:05:41] So you could just actually type in true, but that would be cheating. The way you wanna do is go through and change these three question marks to what, it would be either be null or undefined or some string. And so you go through and then check your answers.

[00:06:00] You can, here let's just make this one false. You save it. And then you go to your spec runner, you refresh your spec runner, and it will show up. So we can see that this one didn't pass. And then if we want it to actually pass, we'll just change it to what it is, we save it, and then we refresh the page, and we see up here, can you guys see that?

[00:06:28] See up here that we have one pass. And this will show you the rest of the ones that you need to change. And it has a little description of the error here.