WEBVTT

00:01.170 --> 00:07.110
You already know that in JavaScript we have function statements and function expressions in case of

00:07.110 --> 00:13.320
function expressions, you're able to assign function as a value to the variable and then invoke it

00:13.320 --> 00:14.360
using variable name.

00:15.120 --> 00:19.110
It's because functions in JavaScript are first class functions.

00:20.250 --> 00:25.710
So I'm not going to repeat again what function expressions are because you already know well how they

00:25.710 --> 00:26.030
work.

00:26.400 --> 00:32.760
I'm just going to show you real power of JavaScript functions which actually act in different ways than

00:32.760 --> 00:34.380
in many other programming languages.

00:35.040 --> 00:37.200
So what are first class functions?

00:38.280 --> 00:45.600
This concept means the following that in JavaScript we are able to store function in a variable we can

00:45.600 --> 00:53.490
use function as a variable, also pass them as parameters and we are able to return function from other

00:53.490 --> 00:56.790
function in many other programming languages.

00:56.800 --> 01:00.060
We are not allowed to use functions in such flexible ways.

01:01.560 --> 01:04.280
All right, let's see exactly what I'm talking about.

01:05.010 --> 01:11.160
So as we said, we can store functions in variables that's demonstrated once again, we need to create

01:11.160 --> 01:13.710
variable and assign to it anonymous function.

01:13.710 --> 01:14.280
So right.

01:14.280 --> 01:18.120
VAR A equals to anonymous function.

01:21.130 --> 01:22.690
And then right, counsel Deathlok.

01:24.120 --> 01:24.540
Hello.

01:28.670 --> 01:35.270
In order to invoke it, we can use the name of the variable and we need to write it after verbal declaration

01:35.270 --> 01:40.430
because of the hoisting, if we invoke it before it's declaration.

01:42.760 --> 01:45.030
Then we will get an error.

01:46.030 --> 01:49.910
Because as we already know, the value of the variable A is undefined.

01:51.220 --> 01:54.430
OK, it was just for reminding some important things.

01:55.120 --> 02:03.940
Let's write another example and remove this code, create function and call it some.

02:05.570 --> 02:06.800
Inside the parentheses.

02:07.760 --> 02:12.080
Let's consider the parameters, number one and number two.

02:14.930 --> 02:21.950
And now I'm going to pass here a third parameter, which I will use then as a function, I mean that

02:21.950 --> 02:26.660
when we invoke function some, we will pass this function as an argument.

02:27.290 --> 02:28.520
So right here from.

02:30.300 --> 02:34.680
Then in council, that's ad number one and number two, right?

02:35.000 --> 02:36.540
Good luck, number one.

02:36.540 --> 02:38.220
Plus number two.

02:40.500 --> 02:47.110
Then write another consider log in, which I'm going to invoke function, which is passed as a parameter.

02:47.700 --> 02:48.870
So right here and.

02:51.330 --> 02:56.580
OK, let's invoke function some rule that we need to write some.

02:58.520 --> 03:08.960
And then pass arguments for number one and number two as five and 10, and now I'm going to pursue as

03:08.960 --> 03:10.730
an argument, anonymous function.

03:11.270 --> 03:21.260
So right to function and then inside the coal prices return statement and write function is executed.

03:26.370 --> 03:35.730
Reload and you see that we have 15 and that function is executed, so you see that we have post function

03:36.120 --> 03:38.640
as a parameter for another function.

03:39.610 --> 03:40.060
OK.

03:41.380 --> 03:47.740
As we said, we can use function as a variable, and in order to do that, we can define this function

03:47.740 --> 03:48.390
outside.

03:48.910 --> 03:50.890
Let's cut it from here and.

03:52.480 --> 03:56.440
Paste, then call it as done.

03:58.560 --> 04:04.740
And then as an argument, we need to pass your name of the function as variable

04:07.380 --> 04:13.260
reload, and you said that we have again, 15 and function is executed.

04:14.830 --> 04:21.910
All right, in order to make our election more interesting, I'm going to write another a bit complicated

04:21.910 --> 04:22.480
example.

04:24.430 --> 04:30.640
Suppose that we have an array which consists of scores of students so.

04:30.640 --> 04:31.060
Right.

04:31.060 --> 04:46.210
Of course, equals and plus here, some numbers, 55, then 35, 87, and let's say 45.

04:48.830 --> 04:54.650
Now we need to create a function which will check if a particular student could pass an exam.

04:55.550 --> 04:59.930
Keep in mind that the limit of passing exam is 51 points.

05:01.310 --> 05:07.490
Recently, I have shot the example like this, but in this case, I'm going to tweet function as first

05:07.490 --> 05:08.180
class function.

05:08.840 --> 05:17.320
So write your function and call it check result inside the parentheses.

05:17.330 --> 05:24.680
I'm going to pass parameters first one for array and the second one for function.

05:26.570 --> 05:32.930
Then inside the the braces create another array which will be empty for now because I'm going to add

05:32.930 --> 05:34.580
in that array results.

05:35.120 --> 05:42.170
So write a new array equals to empty square brackets.

05:45.190 --> 05:51.190
In order to check a particular score, we need to look through the array items for that, I'm going

05:51.190 --> 05:52.240
to use for a loop.

05:52.240 --> 05:53.170
So right for.

05:54.930 --> 05:57.180
Then variable I equal to zero.

05:59.220 --> 06:03.330
Then we need I is less than I write at length.

06:06.400 --> 06:15.160
And then the increment I plus plus, OK, in order to add items, the new empty array, you remember

06:15.160 --> 06:17.560
that we need to use a right push method.

06:18.250 --> 06:21.520
So right newera that push.

06:25.440 --> 06:31.980
And in a moment of adding items, we need to invoke function, which was passed as a parameter here,

06:32.940 --> 06:37.470
as an argument for the function, we need to pass current item from the array.

06:38.280 --> 06:48.320
So right then and inside the parenthesis right away and in square brackets indicate the variable.

06:49.530 --> 06:53.150
And after that, in order to get a new era, we need to return it.

06:53.190 --> 06:53.420
So.

06:53.430 --> 06:53.730
Right.

06:53.910 --> 06:55.470
Return a new array.

06:57.650 --> 06:58.070
OK.

06:59.400 --> 07:05.970
Now, let's write function, which will actually check if a student could pass an exam and then this

07:05.970 --> 07:10.860
function will be passed as an argument when we call check result function.

07:11.520 --> 07:16.590
So write function and call it pass or fail.

07:18.900 --> 07:26.640
As you see, we have passed your parameter, which points to current item, and now we need to indicate

07:26.640 --> 07:27.900
it here as an argument.

07:28.770 --> 07:30.200
Let's call it score.

07:32.990 --> 07:40.190
In order to pass the exam, the current score should be great or equal to 51, so let's write it.

07:40.940 --> 07:47.420
Return score is greater than or equal to 51.

07:48.570 --> 07:52.350
All right, now it's time to check results function.

07:53.650 --> 07:57.790
Write it in console console that lock check result.

08:00.540 --> 08:06.580
So this function has two parameters, array and function as an array.

08:06.600 --> 08:15.030
We need to process course and as function, we need to use pass or fail function that we have just created.

08:18.950 --> 08:27.770
Reload and you see that we have a right which consists of boolean values, true value means that score

08:27.800 --> 08:32.870
was greater or equal to 51 and the student could pass an exam.

08:33.530 --> 08:36.680
And in case of false, the student was failed.

08:38.570 --> 08:39.110
All right.

08:40.930 --> 08:46.330
We can create multiple functions, which will give us other information about the result of the exam,

08:46.690 --> 08:52.930
for example, if we want to define what was the difference between the limit and the actual scores,

08:53.440 --> 08:54.870
we can create another function.

08:55.150 --> 08:56.740
So right function.

08:57.750 --> 09:08.940
And call it calculate difference that inside the parenthesis, right, score again and as a written

09:08.940 --> 09:13.170
statement, we need to write a score minus fifty one.

09:17.540 --> 09:23.990
Then duplicated and changed the name of the function into calculate difference.

09:26.890 --> 09:35.110
Reload, and so here we have another array, which consists of differences between 51 and the actual

09:35.110 --> 09:35.560
scores.

09:37.070 --> 09:39.520
OK, it's nice, right?

09:40.030 --> 09:46.690
Using functions as first class functions is a big advantage of JavaScript because they are very flexible

09:46.690 --> 09:50.680
and convenient way to write really nicely structured code.

09:52.370 --> 09:53.680
All right, let's move on.
