- [Instructor] Hopefully you didn't have any troubles doing
the challenge and the goal was to make you practice
using block scope variables and having them
in different situations,
where they are accessible internally to a block of code.
If you've accomplished this, that's awesome.
Okay, now so let's look at what I did on my side.
So what I did first is to declare an array of warriors
and they are globally block scope available.
So right now we could use within the block
of the entire file, this array.
And then I created a function here
which is also globally available,
so again, they are accessible within the file
because they are in the block scope here.
Then I made sure through a console log
that I had access to the warriors.
So kinda doing the accessibility validation
inside of my function.
So I'm pretty sure that if I run this
I'm gonna see the array but we'll check the browser
in a few seconds.
So I use the function map so I can map or iterate
through the array and then inside of that function
I add a if statement.
And then I pass the warrior agility.
And then in that if statement, if the warrior agility
match 60, then inside, run this code here.
So I've basically created a new variable inside
of that block.
And then I console log what is the warrior name,
the warrior type and then the agility
which I created internally here.
So right now I've created a variable that is only available
within that block, here.
And then I have a variable or an array
that is available across because it is in the global scope.
So if I run this code right now I should see the name
of the warrior that matches the agility at 60.
So let's go in the browser and take a look at the result.
And first, we have a list of the arrays,
so I console log the array which is accessible.
And then, I have Viggo Rusen who is a Viking,
has an agility of 60.
So this is returning the console log with the warrior name,
warrior type and the agility, which is the local
or the block scope variable here.
So let's say, for example, I wanted to use the agility
outside of that block.
The question to yourself is would it work?
Well, the answer is no.
Because if I uncommit this console log here
and I save, I'm gonna get an error.
Reference Error: agility is not defined.
Because it is again, block scope.
The agility has been created here,
then I don't have access to it,
if I did the warrior dot agility, then yes,
I would have access to it.
Because the warrior agility is part
of that block of code here
and accessible.
So if I save that, go back here, I'm gonna get this.
So we're good.
So let's commit the console log.
So if you're having any issues with any of variables
we've explored in this course, local, global
or block scope, go back to the videos where I
explain these concepts and then challenge you again,
until the knowledge sinks in.